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 ?
JSON data has the concept of null and empty arrays and objects.
The JSON Data Interchange Standard definition at json.org specifies that “An object is an unordered [emphasis mine] set of name/value pairs”, whereas an array is an “ordered collection of values”. In other words, by definition the order of the key/value pairs within JSON objects simply does not, and should not, matter.
That JSONObject is actually a map of keys to values. It has no intrinsic order.
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);
}
                        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();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With