有一个 Spring 休息 Controller :
@RestController
@RequestMapping("secanalytique")
public class SectionAnalytiqueController {
@GetMapping(value = "/sectionbyaxepro/{codecomp}", produces = "application/json")
public JSONObject getByAxePro(@PathVariable String codecomp) {
JSONObject jsonModel = new JSONObject();
jsonModel.put("cce0","frityyy");
return jsonModel;
}
}
我用 Postman 做了一个测试:http://172.20.40.4:8080/Oxalys_WS/secanalytique/sectionbyaxepro/8 ;我得到的总是
{
"empty": false
}
那么有什么问题呢?
最佳答案
我遇到了同样的问题,并找到了处理方法。
@GetMapping(value = "/test/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getById(@PathVariable String id) {
JSONObject jsObj = new JSONObject();
jsObj.put("t0","test0");
JSONArray jsArr = new JSONArray();
jsArr.put(jsObj.toMap());
return new ResponseEntity<>(jsObj.toMap(), HttpStatus.OK);
//return new ResponseEntity<>(jsArr.toList(), HttpStatus.OK);
}
关于java - JSONObject 总是返回 "empty": false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683821/