'JSONObject always returns "empty": false
There is a Spring Rest 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;
}
}
I made a test with Postman : http://172.20.40.4:8080/Oxalys_WS/secanalytique/sectionbyaxepro/8 ; and what I got is always
{
"empty": false
}
So what is wrong ?
Solution 1:[1]
I met same issue, and found the way to handle.
@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);
}
Solution 2:[2]
There was one issue with your implementation that you are creating JSON object explicitly and returning it which is not required.
Instead, you should just send your java POJO/class, spring will convert it to JSON and return it.
Spring uses Jackson
as the default serializer/deserializer.
Here since an object is already JSONObject
, Jackson does not know how to serialize it.
There are two ways to solve this problem:
solution 1.
Define your own data type and populate it.
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@GetMapping(value = "/sectionbyaxepro/{codecomp}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> getByAxePro(@PathVariable String codecomp) {
Map<String, String> map = new HashMap<>();
map.put("cce0","frityyy");
return ResponseEntity.status(HttpStatus.OK).body(map);
}
OR
Solution 2.
Modify your existing code to either of the following ways.
1
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@GetMapping(value = "/sectionbyaxepro/{codecomp}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getByAxePro(@PathVariable String codecomp) {
JSONObject jsonModel = new JSONObject();
jsonModel.put("cce0","frityyy");
return ResponseEntity.status(HttpStatus.OK).body(jsonModel.toString());
}
2
@GetMapping(value = "/sectionbyaxepro/{codecomp}", produces = MediaType.APPLICATION_JSON_VALUE)
public String getByAxePro(@PathVariable String codecomp) {
JSONObject jsonModel = new JSONObject();
jsonModel.put("cce0","frityyy");
return jsonModel.toString();
}
Solution 3:[3]
org.json.simple works. For somebody, who does not want to convert the result into String.
Solution 4:[4]
Instead of creating JSONObject manually you can handle it in this way
@GetMapping(value = "/sectionbyaxepro/{codecomp}")
public ResponseEntity<?> getByAxePro(@PathVariable("codecomp") String codecomp){
Map map = new HashMap<>();
map.put("key", "value");
return new ResponseEntity<>(map, HttpStatus.OK);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rocas Yeh |
Solution 2 | |
Solution 3 | ShrutiK |
Solution 4 | Anurag |