【问题标题】:Return JSON for ResponseEntity<String>返回 ResponseEntity<String> 的 JSON
【发布时间】:2013-08-22 15:53:53
【问题描述】:

我的控制器中有一个方法应该返回 JSON 格式的字符串。它为非原始类型返回 JSON:

@RequestMapping(value = "so", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<String> so() {
    return new ResponseEntity<String>("This is a String", HttpStatus.OK);
}

卷曲响应是:

This is a String

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    问题的根源在于 Spring(通过 ResponseEntityRestController 和/或 ResponseBody)将使用字符串的 contents 作为原始响应值,而不是将字符串视为要编码的 JSON 值。即使控制器方法使用produces = MediaType.APPLICATION_JSON_VALUE 也是如此,就像这里的问题一样。

    它本质上就像以下之间的区别:

    // yields: This is a String
    System.out.println("This is a String");
    
    // yields: "This is a String"
    System.out.println("\"This is a String\"");
    

    第一个输出不能解析为 JSON,但第二个输出可以。

    '"'+myString+'"' 之类的东西可能不是一个好主意,因为它无法处理字符串中双引号的正确转义,并且不会为任何此类字符串生成有效的 JSON。

    处理这种情况的一种方法是将字符串嵌入到对象或列表中,这样您就不会将原始字符串传递给 Spring。但是,这会改变您的输出格式,如果您想要这样做,确实没有充分的理由不能返回正确编码的 JSON 字符串。如果这是您想要的,处理它的最佳方式是通过 JSON 格式化程序,例如 JsonGoogle Gson。以下是 Gson 的外观:

    import com.google.gson.Gson;
    
    @RestController
    public class MyController
    
        private static final Gson gson = new Gson();
    
        @RequestMapping(value = "so", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        ResponseEntity<String> so() {
            return ResponseEntity.ok(gson.toJson("This is a String"));
        }
    }
    

    【讨论】:

    • 这是正确答案。您可能应该问自己是否真的需要为这个仅返回字符串的端点支持 JSON。如果不只是将产品更改为 text/plain.
    • 应该被接受为正确答案而不是以前的答案
    【解决方案2】:
    @RequestMapping(value = "so", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String so() {
        return "This is a String";
    }
    

    【讨论】:

    • 这绝对是有效的......你得到了什么作为回应,你到底需要什么?
    • 我得到This is a String,如果我返回一个对象,我得到{"repeatSecond":15}。所以对于原始的,似乎我没有得到一个 JSON 字符串,但是对于我得到的对象。
    • 顺便说一句,我找到了您回答的相关帖子:stackoverflow.com/questions/12911069/…
    【解决方案3】:
    import org.springframework.boot.configurationprocessor.json.JSONException;
    import org.springframework.boot.configurationprocessor.json.JSONObject;
    
    public ResponseEntity<?> ApiCall(@PathVariable(name = "id") long id) throws JSONException {
        JSONObject resp = new JSONObject();
        resp.put("status", 0);
        resp.put("id", id);
    
        return new ResponseEntity<String>(resp.toString(), HttpStatus.CREATED);
    }
    

    【讨论】:

    • @RequestMapping(value = "/ApiCall/{id}", method = RequestMethod.POST,produces = "application/json")
    • org.jsonjson20180813jar
    【解决方案4】:

    另一种解决方案是为String 使用包装器,例如:

    public class StringResponse {
        private String response;
        public StringResponse(String response) {
            this.response = response;
        }
        public String getResponse() {
            return response;
        }
    }
    

    然后在控制器的方法中返回它:

    ResponseEntity<StringResponse>
    

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 2018-09-15
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多