简体   繁体   English

Spring中ResponseEntity和HttpEntity有什么区别?

[英]What's the difference between ResponseEntity and HttpEntity in Spring?

I want to know the difference between the two and when to use them in a SpringMVC Controller. 我想知道两者之间的区别以及何时在SpringMVC Controller中使用它们。

Br,

Tim 提姆

HttpEntity can be used to create both RequestEntity and ResponseEntity. HttpEntity可用于创建RequestEntity和ResponseEntity。

Where as ResponseEntity is subclassed from HttpEntity with a more elaborative way to send the ResponseObject and it only limited to sending the Response. 其中,ResponseEntity是从HttpEntity继承的子类,具有发送发送ResponseObject的更为详尽的方法,并且仅限于发送Response。 Some Key differences are below : 一些主要区别如下:

  1. ResponseEntity inherited from HttpEntity that has an additional HttpStatus Code while Sending the ResponseEntity Object. 从HttpEntity继承的ResponseEntity在发送ResponseEntity对象时具有附加的HttpStatus代码。

  2. Also it has ResponseEntity.BodyBuilder which adds body to the response Object and ResponseEntity.HeadersBuilder which adds header to the Response object. 它还具有将实体添加到响应对象的ResponseEntity.BodyBuilder和将标题添加到响应对象的ResponseEntity.HeadersBuilder。

An HTTP entity is the majority of an HTTP request or response, consisting of some of the headers and the body, if present. HTTP实体是HTTP请求或响应的主要部分,由一些标头和正文(如果存在)组成。 It seems to be the entire request or response without the request or status line (although only certain header fields are considered part of the entity). 它似乎是整个请求或响应,而没有请求或状态行(尽管仅某些标头字段被视为实体的一部分)。

while ResponseEntity is meant to represent the entire HTTP response. 而ResponseEntity则代表整个HTTP响应。 You can control anything that goes into it: status code, headers, and body. 您可以控制其中的所有内容:状态码,标头和正文。

HttpEntity wraps both the request and response message. HttpEntity包装请求和响应消息。 But the ResponseEntity wraps only the response message. 但是ResponseEntity只包装响应消息。 ResponseEntity basically inherits from HttpEntity. ResponseEntity基本上是从HttpEntity继承的。 Response Entity can also have a HTTP status code unlike its parent class. 响应实体也可以具有HTTP状态代码,这与其父类不同。 ex: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ResponseEntity 和 ResponseEntity 有什么区别<myclass>和响应实体?</myclass> - What's the difference between ResponseEntity<MyClass> and ResponseEntity? ResponseEntity 和 ResponseEntity 有什么区别<t>和@ResponseBody?</t> - What is the difference between ResponseEntity<T> and @ResponseBody? Spring-使用HttpEntity从ResponseEntity获取主体的通用方法 - Spring - generic method for getting body from ResponseEntity using HttpEntity Spring BeanFactoryAware和ApplicationContextAware有什么区别? - What's the difference between Spring BeanFactoryAware and ApplicationContextAware? Spring CGLIB 和 CGLIB 有什么区别? - What's the difference between Spring CGLIB and CGLIB? 春天的@Named和@Qualifier有什么区别 - What's the difference between @Named and @Qualifier in spring "Spring 中的@Repository 和@RepositoryDe​​finition 有什么区别?" - What's the difference between @Repository and @RepositoryDefinition in Spring? Spring Data 的 MongoTemplate 和 MongoRepository 有什么区别? - What's the difference between Spring Data's MongoTemplate and MongoRepository? #{} $ {}和%{}之间有什么区别? - what's the difference between #{} ${} and %{}? Spring Boot中的classpath和classpath *有什么区别? - what's the difference between classpath and classpath* in spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM