2016-06-16 2 views
0

私はこれを成功しないと答えてくれます。@RequestMapping(method = RequestMethod.GET)のパラメータとしてJsonオブジェクトを受け取る方法

GETメソッドでJSONとして「複雑な」パラメータを受け取りたいとします。

どうすればいいですか?

@RequestMapping(method = RequestMethod.GET, value = "/test2", consumes = {MediaType.APPLICATION_JSON_VALUE}) 
@ResponseBody 
public ResponseEntity<?> test2(@RequestParam PojoParam pojoParam) { 
    return new ResponseEntity<>(pojoParam, HttpStatus.OK); 
} 

これはPojoParamある:

これは方法である

public class PojoParam implements java.io.Serializable { 
    private String stringVariable; 
    private int intVariable; 

    public PojoParam() { 
    } 

    public String getStringVariable() { 
     return stringVariable; 
    } 

    public int getIntVariable() { 
     return intVariable; 
    } 

    public void setStringVariable(String stringVariable) { 
     this.stringVariable = stringVariable; 
    } 

    public void setIntVariable(int intVariable) { 
     this.intVariable = intVariable; 
    } 
} 

呼び出し例:

... /test2?pojoParam={intVariable:2, stringVariable:"as"} 

応答例:

{ 
    "timestamp": "2016-06-16T15:09:36Z", 
    "status": 500, 
    "error": "Internal Server Error", 
    "exception": "org.springframework.beans.ConversionNotSupportedException", 
    "message": "Failed to convert value of type 'java.lang.String' to required type 'com.htravel.tng.pe.resources.PojoParam'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.htravel.tng.pe.resources.PojoParam]: no matching editors or conversion strategy found", 
    "path": "/rest/availability/test2" 
} 

私は@RequestPartでもやってみました。

提案がありますか?

ありがとうございます。

答えて

0

Stringとして取得してJSON Objectに変換するのはどうですか?

好きなように、hereが挙げられます。

+0

あなたの答えよりも。 私はそのメソッドをPOSTに切り替えて@RequestBodyのようにパラメータをアノテートしても、それは問題なく動作します。 しかし、GETメソッドとして動作し、StringとしてもObjectMapperを使用してもかまいません。 私は今できるとは限りません。 –

+0

ああ、私はあなたがこのパラメータに特定のJavaクラスを持っていないと思っていました。注釈を削除する@RequestParam –

+0

この単純な例ではうまくいきますが、要求は次のようになります。/ test2?intVariable = 2&stringVariable = as OK。しかし、このパラメータがより複雑なオブジェクトである場合、私は同じ問題を抱えています。 Imjoine PojoParamには、別のint変数と別のString変数を持つオブジェクトである別の変数があります。同じ問題。 –

関連する問題