Spring - HttpMessageConversionException using ResponseEntity with Map<String,JSONobject>



我面临一个关于访问Map中的JSON对象的问题<字符串,JSONobject>。

基本上,我试图用这样的控制器方法构建Json回复:

{
"item1": {
"type1": 2,
"type5": 1
},
"item6": {
"type3": 32,
"type26": 7,
"type5": 9
(being the number of keys inside of each key/value an object with several key/value entries)
}
}

这是控制器:

@PostMapping(value="/endpoint")
public ResponseEntity<?> aggData(@RequestBody List<ValidNum> nums){
try {
Map<String, JSONObject> data = dataService.aggData(numsData);
AggSuccess callSuccessResp = new AggSuccess(data);
return new ResponseEntity<>(callSuccessResp, HttpStatus.OK);
} catch (Error e) {
AggError callErrorResp = new AggError(e.getMessage());
return new ResponseEntity<AggregatedDataError>(callErrorResp, HttpStatus.BAD_REQUEST);
}
}

这是服务实现

@Override
public Map<String,JSONObject> aggregateData(List<ValidNum> numsData) {
Map<String,JSONObject> aggData = new HashMap<>();
for (ValidNum vn : numsData) {
if (aggData.get(vn.item) == null) {
JSONObject itemTypesCount = new JSONObject();
aggData.put(vn.item, itemTypesCount.put(vn.type, 1));
} else {
JSONObject itemData = aggData.get(vn.item);
Long typeValue  = (Long) itemData.get(vn.type);
itemData.put(vn.type, typeValue+1);
aggData.put(vn.item, itemData);
}
}
return aggData;
}

ValidNum类

public class ValidNum {
public String nr;
public String item;
public String type;
public ValidNumber(String nr, String item, String type) {
this.nr = nr;
this.item = item;
this.type = type;
}
}

AggSuccess响应类(我认为问题所在(

public class AggSuccess {
public Map<String,JSONObject> aggSuccess;
public AggSuccess(Map<String, JSONObject> aggSuccess) {
this.aggSuccess = aggSuccess;
}
}

问题是,除非我将其添加到应用程序属性中,否则我总是会遇到错误:spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false我不想这样,因为我不想数据看起来像这样:

{
"item1": {},
"item6": {}
}

评论该条目时,我得到以下错误:

ERROR 26551 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.json.JSONObject]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: package.AggDataSuccess["aggSuccess"]->java.util.HashMap["item1"])] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: package.AggDataSuccess["aggSuccess"]->java.util.HashMap["item1"])

最奇怪的是,在控制器的callSuccessResp中调试,就在将数据发送回poster之前,我得到了正确的json值:

{
"item1": {
"type1": 2,
"type5": 1
},
"item6": {
"type3": 32,
"type26": 7,
"type5": 9
}
}

那么,我该怎么做才能让响应实体正确地读取和序列化JSON对象呢?

默认情况下,Jackson不支持org.json类。尝试使用Jackson的com.fasterxml.jackson.databind.node.ObjectNode来构建JSON对象。

在模型中添加默认构造函数以支持反序列化

相关内容

最新更新