2012-04-17 7 views
2

Android用Spring RestTemplateクラスを使用してオブジェクトを送信しています。それは有効な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サポートされていないメディアタイプのエラーが発生してサーバーが復帰しました。

は、念のために、ここにあります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'です。最も奇妙なことは、私はアプリ内の別の場所にオブジェクトを投稿することです。すべての目的と目的のために、これら2つの要求とコードは同一です。他の1つは正常に動作します。 – j0ntech

+0

ええ、これらは追跡するのがかなり難しいです。私はこれを一度だけ持ち、最終的にサーバーのjarとデバッグにバネのソースコードを付けることになりました。 ViewResolversをJacksonJsonViewに正しく設定していなかったため、コンテンツタイプがオーバーライドされたことを覚えています。 – Anirudh

関連する問題