Java @ResponseBody , ResponseEntity Spring 将对象作为 JSON 返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45491308/
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-08-12 02:03:31  来源:igfitidea点击:

@ResponseBody , ResponseEntity Spring return Object as JSON

javaspringspring-mvcspring-data

提问by Mohammad Karmi

I'm using Spring version 4 ( spring data), I want to return Object as JSON , I wonderd that the following code worked even without annotating the user class wtih xmlRootElement:

我正在使用 Spring 版本 4(spring 数据),我想将 Object 作为 JSON 返回,我想知道即使没有使用 xmlRootElement 注释用户类,以下代码也能工作:

@RequestMapping(value = "/resources/users", method = RequestMethod.GET)
public ResponseEntity<User> getUserByLogonId(OAuth2Authentication auth) {

    String userLogonId = ((org.springframework.security.core.userdetails.User) auth.getUserAuthentication()
            .getPrincipal()).getUsername();
    UsersServices usersServices = new UsersServicesImpl(usersOperations);
    User user = usersServices.findByLogonId(userLogonId);
    HttpStatus userStatus = HttpStatus.NOT_FOUND;
    if (user != null) {
        userStatus = HttpStatus.FOUND;
    }
    return new ResponseEntity<User>(user, userStatus);
}

can any body explain ? is ResponseBody/ResponseEntity do the work itself ? when I need to annotate the object class to be returned as JSON.

任何机构都可以解释吗?ResponseBody/ResponseEntity 是自己做的工作吗?当我需要注释要作为 JSON 返回的对象类时。

采纳答案by sunkuet02

@RestControlleritself adds @ResponseBodyannotation. You can see it in the Github Issue

@RestController本身添加了@ResponseBody注释。你可以在Github Issue 中看到

You can also check the official spring tutorials. Here you can check the below lines and examples:

您还可以查看官方 spring 教程。您可以在此处查看以下行和示例:

These controller methods return simple POJOs - Collection<Bookmark>, and Bookmark, etc., in all but the addcase. When an HTTP request comes in that specifies an Accept header, Spring MVCloops through the configured HttpMessageConverteruntil it finds one that can convert from the POJOdomain model types into the content-type specified in the Accept header, if so configured.

这些控制器方法返回简单的 POJO - Collection<Bookmark>、 和Bookmark等,除了这种add情况。当传入指定 的 HTTP 请求时Accept headerSpring MVC循环遍历已配置,HttpMessageConverter直到找到可以从POJO域模型类型转换为 中指定的内容类型Accept header(如果已配置)的请求。

You can also follow the below lines and examples from Official doc

您还可以按照官方文档中的以下行和示例进行操作

@RestController is a stereotype annotation that combines @ResponseBody and @Controller. More than that, it gives more meaning to your Controller and also may carry additional semantics in future releases of the framework.

@RestController 是结合@ResponseBody 和@Controller 的构造型注解。更重要的是,它为您的控制器赋予了更多意义,并且还可能在框架的未来版本中携带额外的语义。

And,

和,

As with @RequestBody and @ResponseBody, Spring uses HttpMessageConverter to convert from and to the request and response streams.

与@RequestBody 和@ResponseBody 一样,Spring 使用 HttpMessageConverter 来转换请求和响应流。

回答by Nikolai

xmlRootElementis using for representing of a object as XML element in an XML document. XML has nothing to do with JSON.
When you annotate your class by @RestControllerit combines @Controllerfor class and @ResponseBodyfor every method. And then HttpMessageConverterconverts from and to the request and response streams.
@ResponseEntityis using for forming http response with custom params (headers, http code and etc.). For example:

xmlRootElement用于将对象表示为 XML 文档中的 XML 元素。XML 与 JSON 无关。
当您通过@RestController@Controller为类和@ResponseBody每个方法注释您的类时。然后HttpMessageConverter在请求和响应流之间进行转换。
@ResponseEntity用于使用自定义参数(标头、http 代码等)形成 http 响应。例如:

return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);

回答by Srinivas Seema

Spring boot uses the Hymanson json libraries for marshalling and unmarshalling the java objects to/from json objects no need any explicit configuration.

Spring Boot 使用 Hymanson json 库将 java 对象编组和解组到 json 对象,无需任何显式配置。