2012-04-17 7 views
2

Android RestTemplate 클래스 용 Spring을 사용하여 객체를 보내고 있습니다. 유효한 JSON (모든 방식으로 확인했습니다)을 보내고 http 헤더와 내용 유형이 올바른 것입니다.서버가 명백한 이유없이 415 HTTP 코드를 반환합니다.

try { 
      Event event = new Event(); 
      // Set event parameters. 
      RestTemplate restTemplate = new RestTemplate(); 
      String url = Const.ADD_EVENT_REQUEST + Const.getRequiredRequestParameters(app); 
      return restTemplate.postForObject(url, event, Boolean.class); 
     } catch (Exception e) { 
      Log.e("Add event task", e.getMessage(), e); 
      return false; 
     } 

서버에서 개체를 받기 : :이 "절약 이벤트"로그가 인쇄되지 않습니다

@RequestMapping(value = "/add", method = RequestMethod.POST) 
    public @ResponseBody Boolean createEvent(@RequestBody Event event) { 
     try { 
      Logger.getLogger(EventRestAction.class).info("saving event " + event); 
      eService.save(event); 
      return true; 
     } catch (Exception e) { 
      Logger.getLogger(EventRestAction.class) 
      .error(e.getMessage(), e); 
      return false; 
     } 
    } 

객체를 보내기. 415 Unsupported media type error (지원되지 않는 미디어 유형 오류)와 함께 서버가 반환됩니다.

그냥 경우에는 여기 년대 dispatcher-servlet.xml :

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

    <context:component-scan base-package="ee.lapikud.ttyapp" /> 
    <mvc:annotation-driven /> 

    <mvc:interceptors> 
     <bean class="ee.lapikud.ttyapp.interceptor.RequestSecurityInterceptor" /> 
    </mvc:interceptors> 


</beans> 

문제는 매우 광범위하지만 꽤 붙어있어 - 어떤이의 원인이 될 수 있을까?

+0

사용하는 스프링의 버전? 3.0.5-RELEASE로 다운 그레이드하여 해결 한 유사한 문제에 대해 고심했습니다. 최신 버전에는 약간의 결함이 있습니다. –

+0

죄송합니다. 1 년 넘게 그 프로젝트를 해왔습니다. 많은 것을 기억하지 못하여 더 이상 세부 사항을 제공 할 수 없습니다. 나는 이것이 결코 해결되지 않았을 것이라고 확신한다. – j0ntech

답변

1

확실히 HTTP 요청/응답의 헤더가 content-type입니다. 두 가지가 모두 application/json인지 확인할 수 있습니까?

UDPATE : 마지막으로 나를 위해 일한 봄 설정 (댓글)

<bean 
     class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <property name="order" value="1"/> 
    <property name="mediaTypes"> 
     <map> 
     <entry key="json" value="application/json"/> 
     </map> 
    </property> 
    <property name="defaultContentType" value="application/json"/> 
    <property name="defaultViews"> 
     <list> 
     <bean 
      class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/> 
     </list> 
    </property> 
    <property name="ignoreAcceptHeader" value="true"/> 
    </bean> 
+0

네, content-type은'application/json'입니다. 가장 이상한 점은 내 앱의 다른 장소에 개체를 게시한다는 것입니다. 모든 의도와 목적을 위해이 두 요청과 코드는 동일합니다. 다른 하나는 잘 작동합니다. – j0ntech

+0

그래, 이것들은 추적하기가 꽤 어렵다. 나는 이것을 한 번 마친 후에 스프링 소스 코드를 서버 항아리에 부착하고 디버깅을 마쳤다. 내 ViewResolvers가 JacksonJsonView에 대해 올바르게 설정되지 않았기 때문에 내용 유형이 무시되는 것을 기억합니다. – Anirudh

관련 문제