2017-12-28 47 views
0
  • 春4.3.2
  • ジャクソン2.7.0

を失敗した私は、春の休みを利用してmicroserviceを作成しました。私は、RequestBodyが入れ子にされたjsonを持つ必要があるという要件を持っています。春休憩サービス@RequestBodyネストされたJSONの構文解析が

RequestWrapper.java:

public class RequestWrapper implements Serializable{ 
private String id; 
private String message; // json in string format 
/** 
getter & setter 
**/ 
} 

メッセージ属性が動的であるが、例えば、一つの要求に対してそれが別のオブジェクトを有していてもよい別の要求foは、「人物」オブジェクトのJSONを有していてもよいです。

{ 
"id":"sample", 
"message":"{\"age\":\"12\"}" 
} 

コントローラーのサンプル::

@Controller 
@EnableWebMvc 
public class RequestController{ 
@RequestMapping(value = "/request.htm", method = RequestMethod.POST, headers = "Accept=*/*", consumes = MediaType.APPLICATION_JSON_VALUE) 
@ResponseBody 
public ResponseWrapper handleRequest(@RequestBody RequestWrapper requestWrapper, 
     HttpServletResponse response) { 
ResponseWrapper responseWrapper = new ResponseWrapper(); 
ObjectMapper mapper = new ObjectMapper(); 
Person person= mapper.readValue(requestWrapper.getMessage(), Person.class); 
    } 
} 

JUnitテストケースでも正常に実行された私は 例を文字列として、ネストされたJSONを持っていたし、後で実際のオブジェクトに変換するとき

すべてがうまく働いていましたこのアプローチで。代わりストリングのオブジェクト

public class RequestWrapper<Object extends Serializable> implements Serializable{ 
private String id; 
    private Object message; // Changed to Object 
    /** 
    getter & setter 
    **/ 
    } 

@Controller 
@EnableWebMvc 
public class CipherController{ 
@RequestMapping(value = "/request.htm", method = RequestMethod.POST, headers = "Accept=*/*", consumes = MediaType.APPLICATION_JSON_VALUE) 
@ResponseBody 
public ResponseWrapper handleRequest(@RequestBody RequestWrapper requestWrapper, 
     HttpServletResponse response) { 
ResponseWrapper responseWrapper = new ResponseWrapper(); 
ObjectMapper mapper = new ObjectMapper(); 
Person person= (Person)requestWrapper.getMessage(); 
} 
} 
以下のよう コントローラクラスを変更よう

は、今は、メッセージ属性のタイプを有するように、RequestWrapper.javaを変更しました

新しいjsonフォーマットは次のようになりました:

例:のpom.xmlで

{ 
"id":"sample", 
"message":{"age":"12"} 
} 

私はJUnitテストケースを実行すると、私は例外の下に取得

WARNING: Failed to read HTTP message: 

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.io.Serializable, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information 
at [Source: [email protected]; line: 1, column: 115] (through reference chain: com.common.model.RequestWrapper["message"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.io.Serializable, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information 

ジャクソン依存

<dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.7.0</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>2.7.0</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-annotations</artifactId> 
     <version>2.7.0</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl --> 
<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-core-asl</artifactId> 
    <version>1.9.13</version> 
</dependency> 
    <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl --> 
<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-mapper-asl</artifactId> 
    <version>1.9.13</version> 
</dependency> 

私はジャクソンパーサと仮定ネストされたjson(メッセージ)がどのオブジェクトインスタンスにマップされるべきかを特定することはできませんでしたが、問題の修正方法はわかりません。

答えて

0

エラーメッセージが表示され、問題が示され、正確な場所が表示されます。

ライン:1、カラム:115](参照チェーンを介し: com.common.model.RequestWrapper [ "メッセージ"])。

構築できません java.io.Serializableを、問題:抽象型カスタムデシリアライザを持って、具体的なタイプにマップする を必要とする、またはそれはあなたのドメインオブジェクトがSerializableを拡張していしても意味がありません追加の型情報

で をインスタンス化することのいずれか。

また、メソッドのフィールドタイプをObjectに変更し、Object.classのStringフィールドとして存在しない "age"の子フィールドを設定しました。これをマッピングする方法はありません.JSON以外のものはそのドメインオブジェクトにとって正当なものではなく、ドメインオブジェクト自体は合法ではありません。

関連する問題