2016-01-03 125 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

像魅力一樣工作。謝謝你,先生 –