I start with learning Spring and I create basic project which creates database, insert values and next print it in web browser. My problem is that when I have RestController in the same package like main class - its OK, but I want distribute it to other package and when I create new package, move the RestController it doesn't work. Let met explain:
My project looks like:
|-Springtestv_01 |-src/main/java |--com.person <-- it's a main package |-Main.java |-Person.java |-PersonLineRunner.java |-PersonRepository.java |-PersonController.java |-com.controller <-- second package, I want put here PersonController.java |-src/main/resources |-data.sql pom.xml
My controller looks:
@RestController public class PersonController { @Autowired PersonRepository personRepository; @RequestMapping("/persons") Collection<Person> persons(){ return this.personRepository.findAll(); } }
When everything is in com.person
package, I write in web brower http://localhost:8080/persons and it works correctly... But I Want move PersonController.java
to com.controller
package, and when I moved it, my webbrowers calls me
There was an unexpected error (type=Not Found, status=404). No message available
and I have no idea what I should do to repair it. Maybe I should change something in my pom.xml
??
My pom.xml looks like
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.person</groupId> <artifactId>person</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringTest_v0_1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.BUILD-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.h2database</groupId><artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-data-elasticsearch </artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
It is generated automatically, I write only one dependency
<dependency> <groupId>com.h2database</groupId><artifactId>h2</artifactId> </dependency>
@RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation. It is a specialized version of @Controller annotation. In @Controller, we can return a view in Spring Web MVC.
So what happens when you have two rest controller defined onto the same path? If you don't have any overlapping request mappings other than the code being slightly confusing, nothing will actually go wrong and you can successfully send requests to the methods inside each controller.
Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.
If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.
Difference between @Controller and @RestController Clearly from above section, @RestController is a convenience annotation that does nothing more than adds the @Controller and @ResponseBody annotations in single statement.
The controller is annotated with the @RestController annotation; therefore, the @ResponseBody isn't required. Every request handling method of the controller class automatically serializes return objects into HttpResponse. 4. Conclusion In this article, we examined the classic and specialized REST controllers available in the Spring Framework.
Spring Framework 5.0 introduced a parallel reactive stack web framework called Spring WebFlux . @RestController is a convenience annotation for creating Restful controllers. It is a specialization of @Component and is autodetected through classpath scanning. It adds the @Controller and @ResponseBody annotations.
The RestController interface is annotated by @Controller and @ResponseBody instead of directly annotating it with @Component. If we replace the annotation of our controller with @RestController, we won't need to change the domain and persistence layer as they still will be compatible with this annotation.
Use basePackages:
@ComponentScan(basePackages = { "com.person","com.controller"} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With