简体   繁体   中英

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 ?

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 way to solve this

  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); }
  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(); }

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);
    }

org.json.simple works. For somebody, who does not want to convert the result into String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM