java 在 ResponseEntity 主体中返回 null(Spring Boot RESTful)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46536208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:15:40  来源:igfitidea点击:

Return null in ResponseEntity body (Spring Boot RESTful)

javaspringrestweb-servicesspring-boot

提问by Hélio Márcio Filho

I'm creating a RESTFul Web Service with the following service available:

我正在创建一个具有以下可用服务的 RESTFul Web 服务:

@RequestMapping(value = "/test", produces = "application/json", method = RequestMethod.GET)
public ResponseEntity<List<GenericModel>> returnEmpty() {
    List<GenericModel> genericModelList = new ArrayList<>();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");

    return responseEntity = new ResponseEntity<>(genericModelList, headers, HttpStatus.OK);
}

When the empty list returns, I'm getting the following response in browser:

当空列表返回时,我在浏览器中收到以下响应:

[]

How can I do to receive in browser a "null" response instead of []? There's some way to tell Spring to render my list size 0 as "null" and not as []?

如何在浏览器中接收“空”响应而不是 []?有什么方法可以告诉 Spring 将我的列表大小 0 呈现为“null”而不是 []?

回答by Naman

One way of pivoting it based on if the genericModelListis empty of not could be:-

一种基于是否genericModelList为空或不为旋转它的方法可能是:-

if(genericModelList.isEmpty()) {
    return new ResponseEntity<>(null, headers, HttpStatus.OK);
} else {
    return new ResponseEntity<>(genericModelList, headers, HttpStatus.OK);
}

or else you can skip the bodyusing ResponseEntity(MultiValueMap<String,String> headers, HttpStatus status)constructor as well.

否则你也可以跳过bodyusingResponseEntity(MultiValueMap<String,String> headers, HttpStatus status)构造函数。

回答by LHCHIN

Because you just initialize the genericModelListas an emptylist, not null. Or you can check the size of list before sending response back with different body.

因为您只是将 初始化genericModelList为一个列表,而不是 null。或者您可以在使用不同的正文发送响应之前检查列表的大小。

回答by Hélio Márcio Filho

Thanks everybody, I will try to be more clear next time. I solved the problem.

谢谢大家,下次我会尽量说得更清楚。我解决了这个问题。

I was trying to send "null" in the body of my ResponseEntity> when my List size is 0, but Hymanson creates a "[]" JSON and not a "null" as I wanted. I didn't found a way to change this default Hymanson's bahavior, so I decided to abandon Hymanson and use the Gson library.

当我的列表大小为 0 时,我试图在我的 ResponseEntity> 的正文中发送“null”,但 Hymanson 创建了一个“[]”JSON 而不是我想要的“null”。我没有找到改变这个默认 Hymanson 行为的方法,所以我决定放弃 Hymanson 并使用 Gson 库。

Let me show a example of my solution:

让我展示一个我的解决方案的例子:

@RequestMapping(value = "/test", produces = "application/json", method = RequestMethod.GET)
public ResponseEntity<String> returnEmpty() {

    List<GenericModel> genericModelList = new ArrayList<>();

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");

    String body = "";

    if (genericModelList.size() == 0) {
        body = "null";
    } else {
        body = gson.toJson(genericModelList);
    }

    return responseEntity = new ResponseEntity<>(body, headers, HttpStatus.OK);
}

This way, returning a String and parsing to JSON myself with Gson instead of use automatic parsing from Spring/Hymanson, I can control the resulting JSON. Look that I explicity return a String "null" if my List.size() == 0.

这样,返回一个字符串并使用 Gson 自己解析为 JSON 而不是使用来自 Spring/Hymanson 的自动解析,我可以控制生成的 JSON。看,如果我的 List.size() == 0,我明确地返回一个字符串“null”。