我的控制器正在接收POST请求作为dto对象,我的dto有一个Object数组,该数组用于获取json对象的数组,我必须向该数组中的每个json对象添加一个键值,然后将其返回。
dto类:
public class FileProcessDTO {
private String module;
private Object[] data;
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public Object[] getData() {
return data;
}
public void setData(Object[] data) {
this.data = data;
}
}
下面是控制器方法
@PostMapping("/processData")
public FileProcessDTO processFileData(@Valid @RequestBody FileProcessDTO fileProcess) throws JSONException {
String module = fileProcess.getModule();
Object[] objects= fileProcess.getData();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray(fileProcess.getData());
FileProcessDTO fileProcessDTO = new FileProcessDTO();
fileProcessDTO.setModule(module);
for (int i = 0; i < jsonArray.length(); i++) {
try {
jsonObject = jsonArray.getJSONObject(i);
jsonObject.put("status", true);
} catch (Exception e) {
jsonObject.put("status", false);
jsonObject.put("error", e.getMessage());
}
objects[i]=jsonObject;
}
fileProcessDTO.setData(objects);
return fileProcessDTO;
}
及以下是来自客户的发布请求中传递的数据
{
"module" : "assignment",
"data":[
{
"name": "chandan",
"age":"27"
},
{
"name": "Yo",
"age":"26"
},
{
"name": "Jo",
"age":"25"
}
]
}
在上述“数据” 对象中,数组键和值在不同的请求中将有所不同(可能在下一个请求中,“名称”和“年龄” 将更改)
下面我收到错误消息。
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.boot.configurationprocessor.json.JSONObject]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.boot.configurationprocessor.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.crisil.apg.service.dto.FileProcessDTO["data"]->org.springframework.boot.configurationprocessor.json.JSONObject[0])
2019-04-17 12:04:14.002 WARN 10808 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator : There was a problem with the instance info replicator
我还尝试如下更改控制器方法中的行,但是,它不作为json响应。
objects[i]=jsonObject.toString();
//below is response
{
"module": "assignment",
"data": [
"{\"name\":\"chandan\",\"age\":\"27\",\"status\":true}",
"{\"name\":\"Yo\",\"age\":\"26\",\"status\":true}",
"{\"name\":\"Jo\",\"age\":\"25\",\"status\":true}"
]
}
请提出可行的代码。谢谢。
答案 0 :(得分:1)
您可以使用String作为请求主体,然后将字符串转换为JSONObject并进行处理。
@PostMapping(value="/get",consumes={"application/json"})
public ResponseEntity<?> test(@RequestBody String req){
//String to JSON Object
JSONObject obj= new JSONObject(req);
JSONArray arr=(JSONArray)obj.get("arr");
//Adding ID to JSON ARRAY
for(int i=0;i<arr.length();i++){
JSONObject ob=(JSONObject)arr.get(i);
ob.append("id", "id-"+i+1);
arr.put(i, ob);
}
return new ResponseEntity<>(arr.toString(),HttpStatus.OK);
}
答案 1 :(得分:0)
我尝试使用您在上面提到的输入来重新创建上述问题,但没有遇到任何错误,我对邮递员的回答如下:
{
"module": "assignment",
"data": [
{
"empty": false
},
{
"empty": false
},
{
"empty": false
}
]
}
登录我的控制台:
2019-04-17 13:24:30.128 DEBUG IND-PBHUITE
--- [nio-8081-exec-3] o.s.w.s.DispatcherServlet : POST "/processData/", parameters={}
2019-04-17 13:24:30.129 DEBUG IND-PBHUITE
--- [nio-8081-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public com.noobcompany.metrostation.model.FileProcessDTO com.noobcompany.metrostation.controller.MainController.processFileData(com.noobcompany.metrostation.model.FileProcessDTO)
2019-04-17 13:24:30.130 DEBUG IND-PBHUITE
--- [nio-8081-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Read "application/json;charset=UTF-8" to [com.noobcompany.metrostation.model.FileProcessDTO@36c525]
2019-04-17 13:24:30.130 DEBUG IND-PBHUITE
--- [nio-8081-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2019-04-17 13:24:30.131 DEBUG IND-PBHUITE
--- [nio-8081-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [com.noobcompany.metrostation.model.FileProcessDTO@f12f53]
2019-04-17 13:24:30.132 DEBUG IND-PBHUITE
--- [nio-8081-exec-3] o.s.w.s.DispatcherServlet : Completed 200 OK
我正在将SpringBoot 2.1.4.RELEASE与RestController一起使用,并对JsonObject使用以下依赖项
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>