springboot设置响应状态码

业务需要自定义http的响应状态,图中响应状态码是200,需要改成401.

使用HttpServletResponse对象直接设置状态局限比较大,可以改用以下方式,通过ResponseEntity可以返回我们需要的数据还可以自定义状态码。

	@RequestMapping("/oauth/token")
	public ResponseEntity<Object> token(@RequestParam(value="grant_type",required=false)String grant_type,
			@RequestParam(value="client_id",required=false)String client_id,
			@RequestParam(value="client_secret",required=false)String client_secret){
		OauthError error=new OauthError();
		AccessTokenResponse tokenResponse=new AccessTokenResponse();
		if(grant_type==null||client_id==null||client_secret==null){
			error.setError(ResponseErrorEnum.INVALID_REQUEST.getError());
			error.setError_description(ResponseErrorEnum.INVALID_REQUEST.getError_description());
			return new ResponseEntity<Object>(error,HttpStatus.OK);
		}
		if(GrantTypeEnum.getGrantTypeEnumByCode(grant_type)==null){
			error.setError(ResponseErrorEnum.UNSUPPORTED_GRANT_TYPE.getError());
			error.setError_description(ResponseErrorEnum.UNSUPPORTED_GRANT_TYPE.getError_description());
			return new ResponseEntity<Object>(error,HttpStatus.OK);
		}
		if(grant_type.equals(GrantTypeEnum.CLIENT_CREDENTIALS.getCode())){
			tokenResponse=oauthService.clientCredentials(client_id, client_secret);
			if(tokenResponse==null){
				error.setError(ResponseErrorEnum.INVALID_CLIENT.getError());
				error.setError_description(ResponseErrorEnum.INVALID_CLIENT.getError_description());
				return new ResponseEntity<Object>(error,HttpStatus.UNAUTHORIZED);
			}
			
		}
		return new ResponseEntity<Object>(tokenResponse,HttpStatus.OK);
	}

运行测试,401

### 如何在Spring Boot中设置HTTP响应头 为了在Spring Boot应用程序中设置HTTP响应头,可以利用`@RestController`中的控制器方法参数`HttpServletResponse`来操作响应对象。另一种更推荐的做法是通过返回`ResponseEntity<?>`类型的对象,在其中指定状态码以及头部信息。 #### 使用 `HttpServletResponse` 当采用这种方式时,可以在处理函数内直接获取到`HttpServletResponse`并调用其提供的API完成自定义响应头的操作[^1]: ```java import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ExampleController { @GetMapping("/example") public String example(HttpServletResponse response) { response.setHeader("Custom-Header", "HeaderValue"); return "Example"; } } ``` #### 返回 `ResponseEntity<?>` 这种方法更加灵活且功能强大,允许开发者不仅能够控制响应体的内容还能精确设定HTTP状态码响应头字段[^2]: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ResponseEntityController { @GetMapping("/response-entity-example") public ResponseEntity<String> responseEntityExample() { HttpHeaders headers = new HttpHeaders(); headers.set("Custom-Header", "HeaderValue"); return new ResponseEntity<>("Example with custom header.", headers, HttpStatus.OK); } } ``` 这两种方式都可以有效地向客户端发送带有特定响应头的数据包。对于RESTful API的设计而言,后者通常被认为是更好的实践因为它提供了更高的抽象层次并且更容易维护和扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值