当前位置:  开发笔记 > 编程语言 > 正文

使用Spring Boot创建RESTful Web服务

如何解决《使用SpringBoot创建RESTfulWeb服务》经验,为你挑选了1个好方法。

我正在使用Spring启动学习RESTful Web服务.我正在尝试创建一个获取特定客户端地址的Web服务.但是,当我尝试运行该服务时,我不断收到以下错误:

白标错误页面

此应用程序没有/ error的显式映射,因此您将此视为回退.

Sun Jan 03 11:20:44 CST 2016出现意外错误(type = Not Found,status = 404).没有可用的消息

我想要访问的URL是

HTTP://本地主机:8084/showAddress

有人可以告诉我哪里出错了.我从朋友的github帐户下载了一个类似的项目,它运行得很好.为了简单起见,我尝试对值进行硬编码,并在我的控制器类中创建以下代码:

package com.digitek.controller;

import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.model.Address;


@RestController
public class Controller {

    private static BigInteger id;
    private static Map addressMap;
    //saves address objects into HashMap
    private static void SaveAddress(Address address){
        //instantiate hashmap when id is null
        if(id == null){
            id = BigInteger.ONE;
            addressMap = new HashMap();
        }
        address.setId(id);
        id.add(BigInteger.ONE);
        addressMap.put(address.getId(), address);
    }

    static{
        Address a1 = new Address();
        a1.setAddress("29 East Judith Ann Drive");
        SaveAddress(a1);

        Address a2 = new Address();
        a1.setAddress("2 East Judith Ann Drive");
        SaveAddress(a2);
    }

    @RequestMapping(value = "/showAddress" ,method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity> showMessage(){
        Collection
address = addressMap.values(); return new ResponseEntity>(address , HttpStatus.OK); } }

这是我的pom.xml文件



    4.0.0

    com.example
    AddressService
    0.0.1-SNAPSHOT
    jar

    AddressService
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.1.RELEASE
         
    

    
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



这是控制台日志

    2016-01-03 11:09:30.359  INFO 6028 --- [           main] com.example.AddressServiceApplication    : Starting AddressServiceApplication on Rishit with PID 6028 (started by Rishit Shah in D:\Rishit\Java workspaces\AddressService) 
2016-01-03 11:09:30.364  INFO 6028 --- [           main] com.example.AddressServiceApplication    : No active profile set, falling back to default profiles: default 
2016-01-03 11:09:30.449  INFO 6028 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33cb5951: startup date [Sun Jan 03 11:09:30 CST 2016]; root of context hierarchy 2016-01-03 11:09:31.655  INFO 6028 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 
2016-01-03 11:09:32.792  INFO 6028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8084 (http) 
2016-01-03 11:09:32.814  INFO 6028 --- [          main] o.apache.catalina.core.StandardService   : Starting service Tomcat 2016-01-03 11:09:32.816  INFO 6028 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30 2016-01-03 11:09:32.965  INFO 6028 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 2016-01-03 11:09:32.965  INFO 6028 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2521 ms 
2016-01-03 11:09:33.628  INFO 6028
    --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/] 
2016-01-03 11:09:33.637  INFO 6028 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*] 
2016-01-03 11:09:33.639  INFO 6028
    --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
2016-01-03 11:09:33.639  INFO 6028 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
 2016-01-03 11:09:33.639  INFO 6028
    --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
 2016-01-03 11:09:34.221  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33cb5951: startup date [Sun Jan 03 11:09:30 CST 2016]; root of context hierarchy 2016-01-03 11:09:34.315  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
 2016-01-03 11:09:34.317  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
 2016-01-03 11:09:34.371  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
 2016-01-03 11:09:34.371  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-01-03 11:09:34.421  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-01-03 11:09:34.588  INFO 6028 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 
2016-01-03 11:09:34.753  INFO 6028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8084 (http) 
2016-01-03 11:09:34.764  INFO 6028 --- [          main] com.example.AddressServiceApplication    : Started AddressServiceApplication in 4.867 seconds (JVM running for 5.705) 
2016-01-03 11:10:03.737  INFO 6028 --- [nio-8084-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet' 2016-01-03 11:10:03.737  INFO 6028 --- [nio-8084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started 2016-01-03 11:10:03.759  INFO 6028 --- [nio-8084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms

PS我尝试在不同的端口上运行应用程序,尝试删除并重新创建它,并尝试运行我从朋友创建的github下载的类似应用程序.每次他的申请工作,但我的申请没有.我还确保我们的pom文件的每个和元素匹配.先感谢您



1> Marlon Berna..:

确保您的主类位于其他类之上的根包中.

当您运行Spring Boot Application(即一个带注释的类@SpringBootApplication)时,Spring将只扫描主类包下面的类.

  com
   +- digitek
         +- Application.java  <--- your main class should be here, above your controller classes
         |
         +- model
         |   +- Address.java
         +- controller
             +- AddressController.java

您的问题是Spring无法找到您创建的控制器,因为您将其放在Spring未扫描的目录中.

文档中有一章解释了如何使用spring boot来构建代码.

推荐阅读
臭小子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有