2016-01-03 83 views
0

我正在使用Spring引导学习REST风格的Web服务。我想创建当我尝试运行服务,我不断收到以下错误获取特定client.However的地址的网络服务:使用Spring引导创建REST风格的Web服务

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jan 03 11:20:44 CST 2016 There was an unexpected error (type=Not Found, status=404). No message available

我试图访问的URL是

http://localhost: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<BigInteger, Address> 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<BigInteger,Address>(); 
     } 
     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<Collection<Address>> showMessage(){ 
     Collection<Address> address = addressMap.values(); 
     return new ResponseEntity<Collection<Address>>(address , HttpStatus.OK); 

    } 



} 

这里是我的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<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.example</groupId> 
    <artifactId>AddressService</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>AddressService</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.1.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

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

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

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

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


</project> 

这里是控制台日志

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[email protected]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[email protected]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<java.util.Map<java.lang.String, java.lang.Object>> 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文件的元素相匹配。 预先感谢您

+0

什么是你试图访问的URL构造代码的章节? – SMA

+0

的URL是http:// localhost:8084/showAddress。我还在问题 –

+0

中更新了错误404表示未找到,因此您没有正确部署。制作一个更简单的.war文件,假设你正在做什么,除了一个基本的index.html页面,并得到这个工作。然后按照你想要的应用程序工作。你是否部署为ROOT.war?如果没有,你可能有一个上下文前缀。你确定服务器配置适合你的端口吗? –

回答

13

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

当您运行Spring Boot应用程序(即用@SpringBootApplication注解的类)时,Spring将只扫描主类包下的类。

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

你的情况的问题在于春找不到您创建的控制器,因为你把它放在哪个不是由春扫描的目录。

有一个在文档解释了如何使用弹簧启动here.

+1

像魅力一样工作。谢谢你,先生 –