美文网首页
rest api 操作 http status code

rest api 操作 http status code

作者: devYOUK | 来源:发表于2018-11-19 18:20 被阅读18次

其实也不限 rest controller
如果要让一个 rest api 更容易做监控的话, 活用 http status code 是一个很便捷的方法

方法1 使用 responseEntity 做返回值
方法的返回值使用 responseEntity
通过调用 responseEntity 中操作 status code 的方法来修改输出时的 status code
也可以使用 ResponseEntity.notFound() 这样的工厂方法

方法2 使用 HttpServletResponse
在方法的形参中添加 HttpServletResponse response
然后在需要的地方 response.setStatus(HttpStatus.SC_NOT_FOUND);
直接操作 status code

方法3 使用 @statusCode 注解
通常还要配合 ExceptionHandler/ControllerAdvice 来使用
这个注解的意思是, 当这个 handlder 方法正常执行了, 会输出什么 status code
一旦定义了, 就是硬编码了, 无法动态修改

相关文章

  • rest api 操作 http status code

    其实也不限 rest controller如果要让一个 rest api 更容易做监控的话, 活用 http st...

  • rest接口返回204

    rest方法如果返回null的话,http status code就是204。前几天前端开发同学说接口返回了204...

  • HTTP Status Code

    说明:

  • HTTP Status Code

    完整的 HTTP 1.1规范说明书来自于RFC 2616,HTTP 1.1的状态码被标记为新特性,用来表示请求的结...

  • http status code

    原文https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html...

  • HTTP Status Code

    http status code分为1xx,2xx,3xx,4xx,5xx 1xx:临时响应,表示临时响应并要求请...

  • HTTP Status Code

    100 ~ 199 信息状态码 100 : Continue 101 : Switching Protocols ...

  • HTTP Status Code

    HTTP Status Code常见的状态码:HTTP: Status 404 – 请求的网页不存在HTTP: S...

  • HTTP STATUS CODE

  • HTTP status code

    HTTP状态码通常分为5种类型,分别以1~5五个数字开头,由3位整数组成: -------------------...

网友评论

      本文标题:rest api 操作 http status code

      本文链接:https://www.haomeiwen.com/subject/nfbafqtx.html