2014-01-17 34 views
1

我已经找了几天,没有运气..我得到一个HTTP 404,并找不到我失踪,为什么它不接我的映射。我做错了吗?春天休息不返回JSON对象 - 越来越404

我正在使用Spring Rest并试图使用@ResponseBody注释返回一个json对象。请帮忙!我使用的是jackson-core-2.2.3.jar,jackson-databind-2.2.3.jar和jackson-annotations-2.2.3.jar。这是我的代码,我的服务器日志如下。感谢帮助!

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1"> 

    <display-name>app</display-name> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/rest/**</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 

    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <context:component-scan base-package="com.app" /> 
    <mvc:annotation-driven/> 

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 

</beans> 

Address.java

package com.app.domain; 

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 


@JsonIgnoreProperties(ignoreUnknown=true) 
public class Address { 

    private String street; 
    private String zip; 
    private String city; 
    private String state; 

    //getters and setters 

} 

AddressController.java

package com.app.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 

import com.app.domain.Address; 

@Controller 
@RequestMapping(value="/rest") 
public class AddressController { 

    @RequestMapping(value="address", method=RequestMethod.GET, headers="application/json") 
    public @ResponseBody Address addressEntry(@RequestParam(value="street", required=true) String street, 
      @RequestParam(value="zip", required=true) String zip){ 

     Address addressRequest = new Address(); 
     addressRequest.setStreet(street); 
     addressRequest.setZip(zip); 
     return addressRequest; 
    } 

    @RequestMapping(value="address/{street}/{zip}", method=RequestMethod.GET, headers="application/json") 
    public @ResponseBody Address addressEntry2(@PathVariable(value="street") String street, 
      @PathVariable(value="zip") String zip){ 


     Address addressRequest = new Address(); 
     addressRequest.setStreet(street); 
     addressRequest.setZip(zip); 
     return addressRequest; 
    } 
} 

我使用JBoss AS7,并在服务器启动时,这里是记录

19:09:22,052 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-8) Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 
19:09:22,430 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 
19:09:22,604 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry(java.lang.String,java.lang.String) 
19:09:22,607 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address/{street}/{zip}],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry2(java.lang.String,java.lang.String) 
19:09:22,640 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final 
19:09:23,102 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address] onto handler 'addressController' 
19:09:23,103 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address.*] onto handler 'addressController' 
19:09:23,104 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/] onto handler 'addressController' 
19:09:23,105 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}] onto handler 'addressController' 
19:09:23,106 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}.*] onto handler 'addressController' 
19:09:23,107 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}/] onto handler 'addressController' 
19:09:23,124 INFO [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization completed in 1169 ms 
19:09:23,173 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/app]] (MSC service thread 1-8) Initializing Spring FrameworkServlet 'dispatcher' 
19:09:23,173 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization started 
19:09:23,177 INFO [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-8) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Jan 16 19:09:23 EST 2014]; parent: Root WebApplicationContext 
19:09:23,180 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-8) Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 
19:09:23,250 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 
19:09:23,290 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry(java.lang.String,java.lang.String) 
19:09:23,291 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address/{street}/{zip}],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry2(java.lang.String,java.lang.String) 
19:09:23,471 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address] onto handler 'addressController' 
19:09:23,472 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address.*] onto handler 'addressController' 
19:09:23,473 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/] onto handler 'addressController' 
19:09:23,473 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}] onto handler 'addressController' 
19:09:23,474 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}.*] onto handler 'addressController' 
19:09:23,475 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}/] onto handler 'addressController' 
19:09:23,507 INFO [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization completed in 333 ms 
19:09:23,514 INFO [org.jboss.web] (MSC service thread 1-8) JBAS018210: Registering web context: /app 
19:09:23,515 INFO [org.jboss.as] (MSC service thread 1-4) JBAS015951: Admin console listening on http://127.0.0.1:9990 
19:09:23,516 INFO [org.jboss.as] (MSC service thread 1-4) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 6174ms - Started 245 of 324 services (78 services are passive or on-demand) 
19:09:23,868 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "appEAR.ear" 

回答

0

也许你应该定义方法应该生产什么。 你可以通过设置RequestMapping注释的produce属性来实现。

实施例: @RequestMapping(值= “地址/ {街道}/{拉链}”,方法= RequestMethod.GET,产生= {MediaType.APPLICATION_JSON_VALUE})

+0

我把你的输入和添加生产= {MediaType.APPLICATION_JSON_VALUE},删除headers =“application/json”属性,然后我得到一个HTTP 406.当我把这两个标题AND产生,我仍然得到404 .. – Bobby

+0

是否有帮助,如果我从我的浏览器显示我的请求标题和响应标题? Content-Type在任何地方都不会说“application/json”。不知道这是否有所作为,如果有,我该如何解决?响应头 的Content-Length Content-Type的\t的text/html;字符集= UTF-8 日期\t周五,2014年1月17日3时55分29秒GMT 服务器\t Apache的狼/ 1.1 请求头 接受\t text/html,application/xhtml + xml,application/xml; q = 0.9,*/*; q = 0.8 – Bobby

+0

您可以看到,header字段accept不包含application/json,因为该方法返回JSON得到一个406.所以如果你需要json,你需要在accept头字段中定义接受application/json,或者如果你可以用xml生活,只需做@RequestMapping(value =“address/{street}/{zip}”,method = RequestMethod.GET,产生= {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICA TION_XML_VALUE}) – iouardi

0

试着改变你的标题属性的值,以

"Accept=application/json" 
+1

从我的理解中,把headers = {“Accept = application/json”}意味着客户端的浏览器必须能够接受application/json。你知道一种方法来强制客户端的浏览器接受application/json吗? – Bobby