Rest Controller does not recognize GET request in Spring Boot App

I am trying to implement a simple MVC demo application using Spring Boot, but when I run the application I get a 404 error. Uri is ` http: // localhost: 8080 / ', which should display all the rows in a table named circle.

  • Spring Download: 1.3.3.RELEASE
  • Java Version: 1.8.0_65
  • Database: Apache Derby 10.12.1.1

Maven Java Project:

Java Maven Project Structure

Application.java

package com.nomad.dubbed.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } } 

CircleController.java

 package com.nomad.dubbed.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.nomad.dubbed.dao.CircleService; import com.nomad.dubbed.model.Circle; @RestController @RequestMapping("/") public class CircleController { @Autowired private CircleService circleService; @RequestMapping(method=RequestMethod.GET) public List<Circle> getAll() { return circleService.getAll(); } } 

CircleRepository.java

 package com.nomad.dubbed.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.nomad.dubbed.model.Circle; @Repository public interface CircleRepository extends JpaRepository<Circle, Integer> { } 

CircleService.java

 package com.nomad.dubbed.dao; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.nomad.dubbed.model.Circle; @Service public class CircleService { @Autowired private CircleRepository circleRepository; @Transactional(propagation=Propagation.REQUIRED) public List<Circle> getAll(){ return circleRepository.findAll(); } } 

Circle.java

 package com.nomad.dubbed.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="circle") public class Circle { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; public Circle(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

application.properties

 spring.datasource.url=jdbc:derby://localhost:1527/db spring.datasource.driverClassName=org.apache.derby.jdbc.ClientDriver logging.level.org.springframework.web:DEBUG logging.level.org.hibernate:DEBUG 

pom.xml

 <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.nomad.dubbed</groupId> <artifactId>spring-boot-mvc</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <derby-client.version>10.11.1.1</derby-client.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-remote-shell</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derbyclient</artifactId> <version>${derby-client.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>spring-boot-mvc</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 

The database is up and running; there are 5 rows in the table circle:

enter image description here

By default, uri (/ beans, / health ..) works fine, but the implemented controller is not recognized. There is no error in the console displayed on the console, below is a log dump printed on the console after sending the request.

 2016-05-03 14:17:26.594 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/] 2016-05-03 14:17:26.596 DEBUG 659 --- [nio-8080-exec-3] swsmmaRequestMappingHandlerMapping : Looking up handler method for path / 2016-05-03 14:17:26.596 DEBUG 659 --- [nio-8080-exec-3] swsmmaRequestMappingHandlerMapping : Did not find handler method for [/] 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] oswshandler.SimpleUrlHandlerMapping : Matching patterns for request [/] are [/**] 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] oswshandler.SimpleUrlHandlerMapping : URI Template variables for request [/] are {} 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] oswshandler.SimpleUrlHandlerMapping : Mapping [/] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[ org.springframework.web.servlet.resource.PathResourceResolver@6c 13019c]]] and 1 interceptor 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : Last-Modified value for [/] is: -1 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : Successfully completed request 2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error] 2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] swsmmaRequestMappingHandlerMapping : Looking up handler method for path /error 2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] swsmmaRequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)] 2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1 2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] oswsvContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html]) 2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] oswsvContentNegotiatingViewResolver : Returning [org.springfram ework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@ 2f5f8d71] based on requested media type 'text/html' 2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : Rendering view [org.springfram ework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@ 2f5f8d71] in DispatcherServlet with name 'dispatcherServlet' 2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] osweb.servlet.DispatcherServlet : Successfully completed request 

Stop application browser

+6
source share
7 answers

use a different url for your controller. "/" in spring-boot for static resources located in meta-INF / resources and src / main / resources / static /.

edit: forget above and follow these steps in your application class:

Application.java

 package com.nomad.dubbed.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @ComponentScan("com.nomad.dubbed") public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } } 

your break controller was not detected when scanning spring-boots components. according to this document http://docs.spring.io/spring-boot/docs/current/reference/html/ ... spring looks at packages under the package where the class with the @SpringBootApplication annotation is located. your controller is in a parallel packet.

+16
source

Could you try adding @ResponseBody Annotation

 @RequestMapping(method=RequestMethod.GET) @ResponseBody public List<Circle> getAll() { return circleService.getAll(); } 
0
source

I need to investigate more why spring loading failed to recognize the controller with the original package structure. I dropped all java classes into one package and finally launched a demo project.

Changed Java project structure:

Changed Java Project Structure

The CircleController.java class has also been modified. I have all the records deleted from the environment table without mentioning a specific request method=RequestMethod.GET , method=RequestMethod.GET .

 package com.nomad.dubbed.app; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class CircleController { @Autowired private CircleService circleService; @RequestMapping(value="/circles", method=RequestMethod.GET) public List<Circle> getAll() { return circleService.getAll(); } } 
0
source

I had the same problem and I added @ComponentScan (basePackages = "package.name") in the Application class. After that, my rest controller was recognized.

package com.nomad.dubbed.app;

 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @ComponentScan(basePackages = "com.spring.basepkg") public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } } 
0
source

In my opinion, this visibility problem occurs when we leave component scanning in Spring, which has a special way of finding classes using the standard convention. In this scenario, since the Starter class (Application) is in the com.nomad.dubbed.app package, placing the controller one level down will help Spring find classes using the default component scanning engine. Putting a CircleController in com.nomad.dubbed.app.controller should solve the problem.

0
source

I had a similar problem. Adding the @SpringBootApplication annotation (scanBasePackages = {"com.nomad.dubbed"}) for the application class worked for me.

0
source

This is what happens behind.

The @SpringBootApplication is a combination of @Configuration @EnableAutoConfiguration @ComponentScan .

@ComponentScan with no arguments tells the platform to search for components / components in the same package and its subpackages.

Your Application class annotated with @SpringBootApplication is in the package com.nomad.dubbed.app . Therefore, it scans this package and its com.nomad.dubbed.app.* (For example, com.nomad.dubbed.app.* ). But your CircleController is inside the com.nomad.dubbed.controller package which is not scanned by default. Your repositories also go beyond the default scan packages, so they will not be detected by the Spring Framework either.

So what to do now? You have two options.

Option 1

Move the Application class to the top directory (package). In your case, the com.nomad.dubbed package. Then, since all controllers and other repositories are in subpackages, they will be discovered by the platform.

Option 2

Use the @ComponentScan annotation with the basePackages argument, along with @SpringBootApplication in your Application class, as shown below.

 @SpringBootApplication @ComponentScan(basePackages="com.nomad.dubbed") public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args); } } 
0
source

All Articles