我正在尝试使用Spring Boot实现简单的演示MVC应用程序,但在执行应用程序时出现404错误。 uri是“http://localhost:8080/”,用于显示名为circle的表中的所有行。
Maven Java Project:
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>
数据库已启动并正在运行,表圈中有5行:
默认的uri(/ beans,/ health ..)工作正常,但无法识别已实现的控制器。控制台中没有显示错误,下面是我发送请求后在控制台中打印的日志转储。
2016-05-03 14:17:26.594 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/]
2016-05-03 14:17:26.596 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /
2016-05-03 14:17:26.596 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/]
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/] are [/**]
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/] are {}
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.handler.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@6c13019c]]] and 1 interceptor
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/] is: -1
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.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] o.s.web.servlet.DispatcherServlet : Successfully completed request
2016-05-03 14:17:26.597 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2016-05-03 14:17:26.600 DEBUG 659 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : 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] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : 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] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.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] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@2f5f8d71] in DispatcherServlet with name 'dispatcherServlet'
2016-05-03 14:17:26.601 DEBUG 659 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Successfully completed request
答案 0 :(得分:16)
为您的控制器使用不同的网址。 &#34; /&#34;在spring-boot中映射到位于META-INF / resources和src / main / resources / static /中的静态资源。
编辑:忘记上面并在您的应用程序类中执行以下操作:
<强> 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);
}
}
弹簧靴组件扫描未发现您的其余控制器。根据此文档http://docs.spring.io/spring-boot/docs/current/reference/html/ ... spring扫描包含@SpringBootApplication批注的类所在的包下面的包。您的控制器位于并行包中。
答案 1 :(得分:4)
我们不应将@ComponentScan批注与@SpringBootApplication一起使用,因为这不是正确的做法。 @SpringBootApplication是03注释@ ComponentScan,@ EnableAutoConfiguration和@Configuration的组合。
答案是Main类,具有@SpringBootApplication批注的应该位于父/超级包中。 例如-com.spring.learning是父包,而child是 com.spring.learning.controller,com.spring.learning.service,com.spring.learning.pojo 因此,它将扫描其程序包和子程序包。 这是正确的做法。项目布局或结构是Spring Boot中一个突出的概念。
答案 2 :(得分:0)
您可以尝试添加@ResponseBody
注释
@RequestMapping(method=RequestMethod.GET)
@ResponseBody
public List<Circle> getAll() {
return circleService.getAll();
}
答案 3 :(得分:0)
我必须研究更多为什么spring-boot无法识别具有原始包结构的控制器。我将所有java类转储到一个包中,最后运行了演示项目。
修改后的Java项目结构:
还修改了CircleController.java类。我从圆桌会议中删除了所有记录,但未提及具体的请求方法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();
}
}
答案 4 :(得分:0)
我遇到了同样的问题,我在Application类中添加了 @ComponentScan(basePackages =&#34; package.name&#34;)。之后,我的休息控制器得到了认可。
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);
}
}
答案 5 :(得分:0)
在我看来,当我们将组件扫描留给Spring时,就会出现这种可见性问题,它具有使用标准约定查找类的特殊方式。在这种情况下,由于Starter类(应用程序)位于com.nomad.dubbed.app软件包中,因此将Controller置于较低级别将有助于Spring使用默认的组件扫描机制查找类。将CircleController放在com.nomad.dubbed.app.controller下应该可以解决此问题。
答案 6 :(得分:0)
我有一个类似的问题。在Application类上添加注释 @SpringBootApplication(scanBasePackages = {“ com.nomad.dubbed”})很适合我。
答案 7 :(得分:0)
这是怎么回事。
@SpringBootApplication
注释是@Configuration
@EnableAutoConfiguration
@ComponentScan
的组合。
@ComponentScan
(不带参数)告诉框架在同一包及其子包中查找组件/ bean。
用Application
注释的@SpringBootApplication
类在包com.nomad.dubbed.app
中。因此,它将扫描该软件包及其下的子软件包(如com.nomad.dubbed.app.*
)。但是您的CircleController
位于软件包com.nomad.dubbed.controller
中,默认情况下不会对其进行扫描。您的存储库也超出了默认的扫描程序包,因此spring框架也不会发现它们。
那现在该怎么办?,您有两个选择。
选项1
将Application
类移动到顶部目录(程序包)。对于您的com.nomad.dubbed
包。然后,由于所有控制器和其他存储库都在子包中,因此框架会发现它们。
选项2
将@ComponentScan
注释与basePackages
参数一起使用,并与@SpringBootApplication
类中的Application
一起使用,如下所示。
@SpringBootApplication
@ComponentScan(basePackages="com.nomad.dubbed")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
答案 8 :(得分:0)
实际上springboots会扫描您的核心软件包下的所有组件,例如:
package com.nomad.dubbed.app;
如果您在com.nomad.dubbed.app.controllers,com.nomad.dubbed.app.services,com.nomad.dubbed.app.dao下添加控制器,服务和dao软件包。
然后,您可以轻松地运行rest控制器,但是如果您将所有与springboot核心软件包平行的软件包(例如com.nomad.dubbed.controllers,com.nomad.dubbed.services)添加。
然后您需要扫描@ComponentScan({“ com.nomad.dubbed.controllers”,“ com.nomad.dubbed.services”})
如果您选择组件,那么还必须扫描springboot应用程序软件包。
最好的方法是在名为boot.app.xyz的春季启动应用程序下创建所有软件包。