i have looked for a few days now and with no luck.. i am getting a http 404 and can not find out what i'm missing, why its not picking up my mapping. am i doing it wrong?
I am using Spring Rest and trying to return a json object using @ResponseBody annotation. Please help! I am using jackson-core-2.2.3.jar, jackson-databind-2.2.3.jar, and jackson-annotations-2.2.3.jar. Here is my code and my server logs are below. appreciate the help!
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>
dispatcher-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;
}
}
I am using Jboss AS7 and when the server starts up, here is the logging
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"
Maybe you should define what the method should produce. you can do that by setting produce attribute for the RequestMapping annotation.
Example: @RequestMapping(value="address/{street}/{zip}", method=RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
尝试将标题属性值更改为
"Accept=application/json"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.