前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ResponseEntity

ResponseEntity

原创
作者头像
用户4396583
发布2024-07-16 15:26:03
830
发布2024-07-16 15:26:03
举报
文章被收录于专栏:spring

ResponseEntity

一、介绍

ResponseEntity继承了HttpEntity类,HttpEntity代表一个http请求或者响应实体,其内部有两个成员变量:header及body,代表http请求或响应的header及body,其中的body是泛化的。

ResponseEntity类,扩展了HttpEntity类,新增了status成员变量,这样,一个ResponseEntity基本可以代表完整的http的请求或响应了。这其实就是ResponseEntity类的作用。

代码语言:java
复制
public class ResponseEntity<T> extends HttpEntity<T> {
 
	private final Object status;
	public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) {
		super(body, headers);
		Assert.notNull(status, "HttpStatus must not be null");
		this.status = status;
	}
    // 省略其他代码
}

二、ResponseEntity常用方法

1、只响应:状态码,不响应数据,如insert、update、delete操作时:

代码语言:java
复制
//方式一,使用ResponseEntiry的静态方法
return ResponseEntity.status(HttpStatus.BAD_REQUEST); 
//方式二,使用ResponseEntiry的静态方法
return ResponseEntity.status(400); 
//方式三,使用ResponseEntity的构造函数,通过数字状态码解析
return new ResponseEntity<>(HttpStatus.resolve(400)); 
//方式四,使用ResponseEntity的构造函数,通过枚举类型获取
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 

以下状态码获取都可以通过这两种方式获得。(数字状态码解析、枚举类型获取)

2、只响应:数据

代码语言:java
复制
//使用ResponseEntiry的静态方法,默认状态码200,pageinfo是分页数据
return ResponseEntity.ok(pageInfo);

3、响应:数据 和 状态码

代码语言:java
复制
//使用ResponseEntity的构造函数
return new ResponseEntity<>(pageInfo,HttpStatus.resolve(200));

4、响应:状态码 和 头信息

代码语言:java
复制
//创建响应头对象,并添加头信息
HttpHeaders headers = new HttpHeaders();
headers.add("Custom-Header", "foo");
//使用ResponseEntity的构造函数
return new ResponseEntity<>(headers,HttpStatus.resolve(200));

5、响应:状态码 、数据 和 头信息

  • 方式一
代码语言:java
复制
//创建响应头对象,并添加头信息
HttpHeaders headers = new HttpHeaders();
headers.add("Custom-Header", "foo");
//使用ResponseEntity的构造函数
return new ResponseEntity<>(pageInfo,headers,HttpStatus.resolve(200));
  • 方式二
代码语言:java
复制
	//创建响应头对象,并添加头信息
	HttpHeaders headers = new HttpHeaders();
	headers.add("Custom-Header", "foo");
	//使用ResponseEntiry的静态方法,默认状态码200
	return ResponseEntity.ok().header("Custom-Header", "001").body(pageInfo);

三、HttpStatus状态码

  • HttpStaus是一个枚举类,包含了所有的状态码信息
代码语言:java
复制
public enum HttpStatus 
  • 常用状态码对应表:其他的自行去HttpStatus枚举中查看

枚举属性

状态码

CONTINUE

100

OK

200

CREATED

201

NO_CONTENT

204

PARTIAL_CONTENT

206

MOVED_PERMANENTLY

301

FOUND

302

NOT_MODIFIED

304

BAD_REQUEST

400

UNAUTHORIZED

401

FORBIDDEN

403

NOT_FOUND

404

INTERNAL_SERVER_ERROR

500

SERVICE_UNAVAILABLE

503

四、补充

  1. ResponseEntity的第一种使用方式
代码语言:java
复制
public class ResponseEntity<T> extends HttpEntity<T> {
    public ResponseEntity(HttpStatus status) {
	    this((Object)null, (MultiValueMap)null, (HttpStatus)status);
	}
	
   public ResponseEntity(@Nullable T body, HttpStatus status) {
        this(body, (MultiValueMap)null, (HttpStatus)status);
    }
	
   public ResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) {
        this((Object)null, headers, (HttpStatus)status);
    }
	
   public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) {
        super(body, headers);
        Assert.notNull(status, "HttpStatus must not be null");
        this.status = status;
    }
 
   private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, Object status) {
        super(body, headers);
        Assert.notNull(status, "HttpStatus must not be null");
        this.status = status;
    }
}

可以看到这个类提供了五个构造方法,返回的状态码是必传的外,头部信息以及响应体内容都是可选择的。当我们需要使用的时候,直接new一个ResponseEntity对象作为API返回值即可,这就是它的第一种使用方式。

需要注意的是,在有headers作为参数的构造方法中,需要传入一个类型为MultiValueMap<String, String>的参数。MultiValueMap继承自Map这个抽象类,其中拥有一个叫做HttpHeaders的子类,我们可以当它为一个key和value都为String类型的HashMap使用。HttpHeaders里面保存了一些常用的Header的key值,例如"Accept-Charset"。当然也可以自定义一些特殊的key。

代码语言:java
复制
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin");
 
return new ResponseEntity<>(resultBody, headers, HttpStatus.OK);
  1. ResponseEntity的第二种使用方式 继续看源码,发现了好多返回值为ResponseEntity.BodyBuilder的方法有木有
代码语言:java
复制
    public static ResponseEntity.BodyBuilder status(HttpStatus status) {
        Assert.notNull(status, "HttpStatus must not be null");
        return new ResponseEntity.DefaultBuilder(status);
    }
 
    public static ResponseEntity.BodyBuilder status(int status) {
        return new ResponseEntity.DefaultBuilder(status);
    }
        public static ResponseEntity.BodyBuilder ok() {
        return status(HttpStatus.OK);
    }
    public static ResponseEntity.BodyBuilder created(URI location) {
        ResponseEntity.BodyBuilder builder = status(HttpStatus.CREATED);
        return (ResponseEntity.BodyBuilder)builder.location(location);
    }
 
    public static ResponseEntity.BodyBuilder accepted() {
        return status(HttpStatus.ACCEPTED);
    }
 
    public static ResponseEntity.HeadersBuilder<?> noContent() {
        return status(HttpStatus.NO_CONTENT);
    }
 
    public static ResponseEntity.BodyBuilder badRequest() {
        return status(HttpStatus.BAD_REQUEST);
    }
 
    public static ResponseEntity.HeadersBuilder<?> notFound() {
        return status(HttpStatus.NOT_FOUND);
    }
 
    public static ResponseEntity.BodyBuilder unprocessableEntity() {
        return status(HttpStatus.UNPROCESSABLE_ENTITY);
    }
    //.........

事实证明,越是伟大的软件工程师是越“懒惰”的,为了节约时间增加效率,我们可以用Builder的方式去生成ResponseEntity对象并返回:

代码语言:java
复制
Map<String, String> resultBody = new HashMap<>();
resultBody.put("name":"Lucas");
resultBody.put("school":"harvard university");
return ResponseEntity.status(HttpStatus.OK).body(resultBody);

与其他方式对比

  • @RestController注解修饰controller层。

@RestController = @Controller + @ResponseBody

@ResponseBody是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。

注意:在使用此注解之后不会再走视图处理器(ModelAndView),而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。

  • @ResponseStatus

这个注解主要用在自定义的Exception 上,或者直接用在controller层的API方法上也可,当发生异常/方法执行结束时,会返回相应的Http状态码和msg。

代码语言:java
复制
@ResponseStatus(value=HttpStatus.FORBIDDEN,reason="不允许访问")

注:

ResponseEntity的优先级高于@ResponseBody。只有在返回值不为ResponseEntity的情况下才去检查有没有@ResponseBody注解;如果响应类型是ResponseEntity则会忽略@ResponseBody注解。

总结

ResponseEntity能够非常方便的修改返回值的状态码,但最优秀的用法仍然是能够为不同API设置不同的返回响应头。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • ResponseEntity
  • 一、介绍
  • 二、ResponseEntity常用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档