我已经找了几天而且没有运气..我得到了一个http 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 :(得分:0)
也许你应该定义方法应该产生什么。 你可以通过为RequestMapping注释设置产生属性来做到这一点。
实施例: @RequestMapping(value =“address / {street} / {zip}”,method = RequestMethod.GET,produce = {MediaType.APPLICATION_JSON_VALUE})
答案 1 :(得分:0)
尝试将标题属性值更改为
"Accept=application/json"