- 春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(メッセージ)がどのオブジェクトインスタンスにマップされるべきかを特定することはできませんでしたが、問題の修正方法はわかりません。