【问题标题】:SpingBoot Mapping not working. 404 statusSpring Boot 映射不起作用。 404状态
【发布时间】:2015-11-01 12:42:09
【问题描述】:

我正在逐步关注 youtube 上的教程,但没有得到结果。代码如下:

主要应用

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

模型类

public class Greeting {

    private BigInteger id;
    private String text;

    public Greeting() {}

    public BigInteger getId() {
        return id;
    }

    public void setId(BigInteger id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

问候控制器

@RestController()
public class GreetingController {

    private static BigInteger nextId;
    private static Map<BigInteger, Greeting> greetingMap;

    private static Greeting save(Greeting greeting) {
        if (greetingMap == null) {

            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;
        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World");
        save(g1);
        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo!");
        save(g2);
    }

    @RequestMapping(
            value = "/api/greetings",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Greeting>> getGreetings() {
        Collection<Greeting> greetings = greetingMap.values();

        return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);
    }
}

Pom.xml 文件

<modelVersion>4.0.0</modelVersion>

<groupId>com.hakeem.webservice2</groupId>
<artifactId>WebService2</artifactId>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.1.RELEASE</version>
</parent>

<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>

</build>

每当我运行一个 get 请求时,我都会从 Postman 那里得到这个响应:

{
    "timestamp": 1439128293727,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/api/greetings"
}

或来自 chrome 的以下内容:

白标错误页面

此应用程序没有显式映射 /error,因此您将其视为后备。

2015 年 8 月 9 日星期日 08:43:41 COT 出现意外错误(类型=未找到,状态=404)。 没有可用的消息

我一步一步地跟着视频。视频上的导师似乎没有做任何额外的事情。 每当我输入

localhost:8080/api/greetings

我得到的只是上述错误。

加法。这是控制台输出,不知道有没有用:

2015-08-09 10:49:09.918 INFO 7696 --- [main] com.hakeem.webservice2.Application:在 PID 7696 的笔记本电脑上启动应用程序(C:\Users\HakeemAbdussamad\Documents\MarsWorkspace\WebService2\ target\classes 由 HakeemAbdussamad 在 C:\Users\HakeemAbdussamad\Documents\MarsWorkspace\WebService2 中启动 2015-08-09 10:49:09.988 INFO 7696 --- [main] ationConfigEmbeddedWebApplicationContext:刷新 org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bb0e64a:启动日期 [Sun Aug 09 10:49:09 COT 2015];上下文层次的根 2015-08-09 10:49:10.710 INFO 7696 --- [main] o.s.b.f.s.DefaultListableBeanFactory:覆盖 bean 'beanNameViewResolver' 的 bean 定义:替换 [Root bean: class [null];范围=;摘要=假;懒惰初始化=假;自动线模式=3;依赖检查=0;自动接线候选=真;主要=假; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver;初始化方法名=空; destroyMethodName=(推断);在类路径资源 [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] 中定义为 [Root bean: class [null];范围=;摘要=假;懒惰初始化=假;自动线模式=3;依赖检查=0;自动接线候选=真;主要=假; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver;初始化方法名=空; destroyMethodName=(推断);在类路径资源 [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class] 中定义 2015-08-09 10:49:11.547 INFO 7696 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat 使用端口初始化:8080 (http) 2015-08-09 10:49:11.797 INFO 7696 --- [main] o.apache.catalina.core.StandardService:启动服务 Tomcat 2015-08-09 10:49:11.801 INFO 7696 --- [main] org.apache.catalina.core.StandardEngine:启动 Servlet 引擎:Apache Tomcat/8.0.15 2015-08-09 10:49:11.926 INFO 7696 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]:初始化 Spring 嵌入式 WebApplicationContext 2015-08-09 10:49:11.926 INFO 7696 --- [ost-startStop-1] os.web.context.ContextLoader:根 WebApplicationContext:初始化在 1942 毫秒内完成 2015-08-09 10:49:12.743 信息 7696 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean:映射 servlet:'dispatcherServlet' 到 [/] 2015-08-09 10:49:12.747 信息 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:映射过滤器:'characterEncodingFilter' 到:[/] 2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:映射过滤器:'hiddenHttpMethodFilter' 到:[/] 2015-08-09 10:49:12.963 INFO 7696 --- [main] swsmmaRequestMappingHandlerAdapter: 寻找@ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bb0e64a: 启动日期 [Sun Aug 09 10:49: 09 COT 2015];上下文层次的根 2015-08-09 10:49:13.035 INFO 7696 --- [main] swsmmaRequestMappingHandlerMapping:映射“{[/error],methods=[],params=[],headers=[],consumes=[],produces =[],custom=[]}" 到公共 org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2015-08-09 10:49:13.036 INFO 7696 --- [main] swsmmaRequestMappingHandlerMapping:映射“{[/error],methods=[],params=[],headers=[],consumes=[],产生=[text/html],custom=[]}" 到公共 org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest) 2015-08-09 10:49:13.108 INFO 7696 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping:将 URL 路径 [/] 映射到 [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 类型的处理程序上 2015-08-09 10:49:13.108 INFO 7696 --- [main] oswshandler.SimpleUrlHandlerMapping:将 URL 路径 [/webjars/] 映射到 [class org.springframework.web.servlet.resource .ResourceHttpRequestHandler] 2015-08-09 10:49:13.191 INFO 7696 --- [main] oswshandler.SimpleUrlHandlerMapping:将 URL 路径 [/**/favicon.ico] 映射到 [class org.springframework.web.servlet.resource .ResourceHttpRequestHandler] 2015-08-09 10:49:13.276 INFO 7696 --- [main] o.s.j.e.a.AnnotationMBeanExporter:在启​​动时为 JMX 暴露注册 bean 2015-08-09 10:49:13.329 INFO 7696 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat 在端口:8080 (http) 上启动 2015-08-09 10:49:13.331 INFO 7696 --- [main] com.hakeem.webservice2.Application:在 3.733 秒内启动应用程序(JVM 运行 4.32)

【问题讨论】:

    标签: java maven spring-boot


    【解决方案1】:

    给定的代码在我看来是正确的。查看完整的源代码可能会有所帮助。例如 - 如果GreetingController 类不在同一个包或Application 类的子包中,则不会对其进行扫描,因此会出现此错误。

    【讨论】:

    • Application Greetings 和 Greetings Controller 在 3 个不同的包中。
    • 那就是问题所在。您应该将所有组件(即控制器、服务等)放在使用 SpringBootApplication 注释的 Application 类的同一包或子包中。 (否则你将不得不使用@ComponentScan...)
    • 谢谢。有效。但是,在视频中,导师说注解 SpringBootApplication 替代了其他三个注解。具体来说:ComponentScan、EnableAutoConfiguration 和 Configuration。他是否犯了错误,或者他的方法是否应该在其他情况下起作用。我问是因为他的应用程序可以使用三个不同的包。所以也许我错过了什么。
    • 他说得对。但是 ComponentScan 默认只扫描同一个包和子包中的组件。如果要扫描其他包,则需要将包作为参数传递给@ComponentScan。
    • 阿布,你可能想投票和/或接受答案(点击向上三角形投票;点击勾选接受),假设它有帮助。
    猜你喜欢
    • 2016-12-14
    • 2021-08-10
    • 2021-06-24
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2016-12-24
    相关资源
    最近更新 更多