java - JSONObject 总是返回 "empty": false

标签 java json spring rest

有一个 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/

相关文章:

javascript - foreach for JSON 数组,语法

java - 使用 Gson TypeAdapter 以顺序不敏感的方式反序列化 JSON 对象

javascript - 如何让 JSON blob 中的 HTML 正确呈现?

spring - 不能在 Spring AWS 示例上强制转换为 org.springframework.core.io.WritableResource

java - Spring Security — 'Full authentication is required to access this resource' 表示不存在的端点

java - 将 HTTP header 添加到 JAX-WS 服务响应

java - 为什么创建了一个JFrame对象并设置为可见后,程序还没有结束执行?

java - 如何通过hibernate检查数据库是否启动?

java - Spring 引导中的 TLSv1.2 错误

java - 使用 Controller、Service 和 Repository 注释的最佳实践是什么?