나는 운좋게도 며칠을 보았습니다. http 404를 얻었습니다. 내가 누락 된 부분을 찾을 수 없었습니다. 왜 내 매핑을 선택하지 않는지 알 수 없습니다. . 내가 잘못하고있는거야?spring rest가 json 객체를 반환하지 않음 - 점점 404
저는 Spring Rest를 사용하고 @ResponseBody annotation을 사용하여 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;
}
}
,174,
나는 제이 보스의 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"
내가 입력을 가져다가 내가 헤더를 모두 넣으면 = {MediaType.APPLICATION_JSON_VALUE가}, 헤더 = "응용 프로그램/JSON"속성을 제거 후, 나는 HTTP (406)를 얻을 생산하고 추가 및 생산, 난 여전히 404을 얻을 .. – Bobby
내 브라우저에서 요청 헤더와 응답 헤더를 표시하면 도움이됩니까? Content-Type은 "application/json"을 아무 데서도 말하지 않습니다. 차이가 있는지 확실하지 않은 경우, 그렇지 않으면 어떻게 수정합니까? 응답 헤더 콘텐츠 길이 콘텐츠 유형 \t text/html과; 문자셋 = UTF-8 날짜 \t 2014년 (금) 1월 17일 그리니치 표준시 03시 55분 29초 서버 \t 아파치 - 코요테/1.1 요청 헤더 \t을 수락 text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8 – Bobby
알다시피, 헤더 필드 accept는 application/json을 포함하지 않으며 JSON을 반환하기 때문에 get json이 필요하면 accept 헤더 필드에서 application/json을 수락하도록 정의하거나 xml로 살 수 있다면 @RequestMapping (value = "address/{street}/{zip}", method = RequestMethod.GET, = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICA TION_XML_VALUE}) – iouardi