Rest Controller无法在Spring Boot App中识别GET请求

gkc123

我正在尝试使用Spring Boot实现简单的演示MVC应用程序,但执行该应用程序时出现404错误。uri是“ http:// localhost:8080 / ”,用于显示称为圆的表中的所有行。

  • 春季靴:1.3.3。发布
  • Java版本:1.8.0_65
  • 数据库:Apache Derby 10.12.1.1

Maven Java项目:

Maven 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 

Rest App浏览器

鲁兹·穆勒(LutzMüller)

为您的控制器使用其他网址。spring-boot中的“​​ /”映射到位于META-INF / resources和src / main / resources / static /中的静态资源。

编辑:忘记上面的内容,并在您的应用程序类中执行以下操作:

应用程序

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);
    }

}

spring-boots组件扫描未发现您的rest控制器。根据此文档,http://docs.spring.io/spring-boot/docs/current/reference/html/…spring扫描具有@SpringBootApplication批注的类所在的软件包下面的软件包。您的控制器位于并行包装中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring Boot REST API-请求超时?

来自分类Dev

Spring Boot Rest服务| 请求方法'GET'不支持

来自分类Dev

用于Spring Boot Rest Controller的Junit

来自分类Dev

REST Spring Boot中复合主键的用法

来自分类Dev

Spring Boot REST API中的对象数组

来自分类Dev

Spring Boot无法启动REST服务

来自分类Dev

无法运行 Spring Boot 简单 REST 服务

来自分类Dev

使用Spring Security保护rest-api后,Spring Boot JS App无法正常工作

来自分类Dev

如何在使用 spring boot 的 REST Web 服务中的 GET 请求期间修复“HTTP-404”错误

来自分类Dev

Spring Boot定义了REST接口,想定时请求-响应

来自分类Dev

如何使用Spring Boot验证每个REST请求

来自分类Dev

无法在 Spring Boot 中通过 RestTemplate 和 Eureka 使用 REST API

来自分类Dev

无法识别Spring Boot服务

来自分类Dev

如何为特定的Rest Controller禁用Spring Boot Authentication控制

来自分类Dev

Spring Boot Rest 服务 Angular

来自分类Dev

Spring Boot Rest:json 问题

来自分类Dev

Rest Service中的Spring Boot自定义异常

来自分类Dev

在Spring Boot中通过其REST API URL调用函数

来自分类Dev

Spring Boot REST仅在JSON响应中显示父代的ID

来自分类Dev

在tomcat上的Spring Boot Rest Service部署:无法找到JDBC

来自分类Dev

无法通过Spring Boot调用https REST端点

来自分类Dev

Spring Boot无法访问REST控制器

来自分类Dev

无法测试使用Spring Boot开发的REST API

来自分类Dev

Spring boot 2.0.0.BUILD-SNAPSHOT rest:无法覆盖_serializer?

来自分类Dev

Spring-boot REST 安全配置角色无法正常工作

来自分类Dev

GAE上的Spring Boot Rest App引发异常,无法将其强制转换为javax.persistence.EntityManagerFactory”

来自分类Dev

使用文本文件中的每个请求的会话 ID 保存请求和响应,REST API Spring Boot

来自分类Dev

Spring Rest Controller 不适用于 weblogic 10.3 / Spring boot

来自分类Dev

Spring Boot REST @RequestParam未通过验证

Related 相关文章

  1. 1

    Spring Boot REST API-请求超时?

  2. 2

    Spring Boot Rest服务| 请求方法'GET'不支持

  3. 3

    用于Spring Boot Rest Controller的Junit

  4. 4

    REST Spring Boot中复合主键的用法

  5. 5

    Spring Boot REST API中的对象数组

  6. 6

    Spring Boot无法启动REST服务

  7. 7

    无法运行 Spring Boot 简单 REST 服务

  8. 8

    使用Spring Security保护rest-api后,Spring Boot JS App无法正常工作

  9. 9

    如何在使用 spring boot 的 REST Web 服务中的 GET 请求期间修复“HTTP-404”错误

  10. 10

    Spring Boot定义了REST接口,想定时请求-响应

  11. 11

    如何使用Spring Boot验证每个REST请求

  12. 12

    无法在 Spring Boot 中通过 RestTemplate 和 Eureka 使用 REST API

  13. 13

    无法识别Spring Boot服务

  14. 14

    如何为特定的Rest Controller禁用Spring Boot Authentication控制

  15. 15

    Spring Boot Rest 服务 Angular

  16. 16

    Spring Boot Rest:json 问题

  17. 17

    Rest Service中的Spring Boot自定义异常

  18. 18

    在Spring Boot中通过其REST API URL调用函数

  19. 19

    Spring Boot REST仅在JSON响应中显示父代的ID

  20. 20

    在tomcat上的Spring Boot Rest Service部署:无法找到JDBC

  21. 21

    无法通过Spring Boot调用https REST端点

  22. 22

    Spring Boot无法访问REST控制器

  23. 23

    无法测试使用Spring Boot开发的REST API

  24. 24

    Spring boot 2.0.0.BUILD-SNAPSHOT rest:无法覆盖_serializer?

  25. 25

    Spring-boot REST 安全配置角色无法正常工作

  26. 26

    GAE上的Spring Boot Rest App引发异常,无法将其强制转换为javax.persistence.EntityManagerFactory”

  27. 27

    使用文本文件中的每个请求的会话 ID 保存请求和响应,REST API Spring Boot

  28. 28

    Spring Rest Controller 不适用于 weblogic 10.3 / Spring boot

  29. 29

    Spring Boot REST @RequestParam未通过验证

热门标签

归档