JSONObject始终返回"empty":false [英] JSONObject always returns "empty": false

查看:896
本文介绍了JSONObject始终返回"empty":false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Spring Rest控制器:

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

}

我对邮递员进行了测试: http://172.20.40.4:8080/Oxalys_WS /secanalytique/sectionbyaxepro/8 ;而我得到的总是

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
}

那怎么了?

推荐答案

我遇到了同样的问题,并找到了解决方法.

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

这篇关于JSONObject始终返回"empty":false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆