SpingBoot映射无法正常工作。 404状态

时间:2015-08-09 14:37:30

标签: java maven spring-boot

我正在逐步关注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的

或以下内容:

  

Whitelabel错误页面

     

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

     

Sun Aug 09 08:43:41 COT 2015   出现意外错误(type = Not Found,status = 404)。   没有可用的消息

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

  

本地主机:8080 / API /问候

我得到的只是前面提到的错误。

的增添。这是控制台输出,我不知道它是否有用:

  

2015-08-09 10:49:09.918 INFO 7696 --- [main] com.hakeem.webservice2.Application:使用PID 7696在笔记本电脑上启动应用程序(C:\ Users \ HakeemAbdussamad \ Documents \ MarsWorkspace \ WebService2 \目标\类由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的bean定义beanNameViewResolver&#39 ;: replacement [root bean:class [null];范围=;抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE;初级= FALSE; factoryBeanName = org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration $ WhitelabelErrorViewConfiguration; factoryMethodName = beanNameViewResolver; initMethodName = NULL; destroyMethodName =(推断);在类路径资源[org / springframework / boot / autoconfigure / web / ErrorMvcAutoConfiguration $ WhitelabelErrorViewConfiguration.class]中定义[Root bean:class [null];范围=;抽象= FALSE; lazyInit = FALSE; autowireMode = 3; dependencyCheck = 0; autowireCandidate = TRUE;初级= FALSE; factoryBeanName = org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration $ WebMvcAutoConfigurationAdapter; factoryMethodName = beanNameViewResolver; initMethodName = NULL; 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] o.s.web.context.ContextLoader:Root WebApplicationContext:初始化于1942年完成   2015-08-09 10:49:12.743 INFO 7696 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean:映射servlet:&#39; dispatcherServlet&#39;至 [/]   2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:映射过滤器:&#39; characterEncodingFilter&#39;至:[/ ]   2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:映射过滤器:&#39; hiddenHttpMethodFilter&#39;致:[/ ]   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:Mapped&#34; {[/ error],methods = [],params = [],headers = [],consume = [ ],产生= [],定制= []}&#34; on public org.springframework.http.ResponseEntity&gt; org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)   2015-08-09 10:49:13.036 INFO 7696 --- [main] swsmmaRequestMappingHandlerMapping:Mapped&#34; {[/ error],methods = [],params = [],headers = [],consume = [ ],产生= [text / html的],定制= []}&#34; on public 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)

1 个答案:

答案 0 :(得分:4)

给定的代码看起来正确。查看完整的来源可能会有所帮助。例如 - 如果Application类不在同一个包中或row.setGravity(Gravity.CENTER_HORIZONTAL); 类的子包中,则不会扫描它,因此您将出现此错误。