@ResponseStatus是标记一个方法或异常类在返回时响应的http状态。其代码注释如下:
*
* <p>The status code is applied to the HTTP response when the handler
* method is invoked and overrides status information set by other means,
* like {@code ResponseEntity} or {@code "redirect:"}.
*
* <p><strong>Warning</strong>: when using this annotation on an exception
* class, or when setting the {@code reason} attribute of this annotation,
* the {@code HttpServletResponse.sendError} method will be used.
*
* <p>With {@code HttpServletResponse.sendError}, the response is considered
* complete and should not be written to any further. Furthermore, the Servlet
* container will typically write an HTML error page therefore making the
* use of a {@code reason} unsuitable for REST APIs. For such cases it is
* preferable to use a {@link org.springframework.http.ResponseEntity} as
* a return type and avoid the use of {@code @ResponseStatus} altogether.
*
* <p>Note that a controller class may also be annotated with
* {@code @ResponseStatus} and is then inherited by all {@code @RequestMapping}
* methods.
@ResponseStatus可以结合@ResponseBody一起使用。
@RequestMapping("/testResponseBody")
@ResponseBody
public Map<String, String> testResponseBody(){
return ImmutableMap.of("key", "value");
}
@RequestMapping("/testResponseBodyFaild")
@ResponseBody
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public Map<String, String> testResponseBodyFaild(){
return ImmutableMap.of("key", "faild");
}
这两个handler方法返回的http状态码(http状态码)分别是200和501。
ResponseEntity是在org.springframework.http.HttpEntity的基础上添加了http状态码(http状态码),用于RestTemplate以及@Controller的HandlerMethod。它在控制器中或用于服务端响应时,作用是和@ResponseStatus与@ResponseBody结合起来的功能一样的。用于RestTemplate时,它是接收服务端返回的http状态码和reason的
代码。注释如下:
* Extension of {@link HttpEntity} that adds a {@link HttpStatus} status code.
* Used in {@code RestTemplate} as well {@code @Controller} methods.
*
* <p>In {@code RestTemplate}, this class is returned by
* {@link org.springframework.web.client.RestTemplate#getForEntity getForEntity()} and
* {@link org.springframework.web.client.RestTemplate#exchange exchange()}:
* <pre class="code">
* ResponseEntity<String> entity = template.getForEntity("http://example.com", String.class);
* String body = entity.getBody();
* MediaType contentType = entity.getHeaders().getContentType();
* HttpStatus statusCode = entity.getStatusCode();
* </pre>
*
* <p>Can also be used in Spring MVC, as the return value from a @Controller method:
* <pre class="code">
* @RequestMapping("/handle")
* public ResponseEntity<String> handle() {
* URI location = ...;
* HttpHeaders responseHeaders = new HttpHeaders();
* responseHeaders.setLocation(location);
* responseHeaders.set("MyResponseHeader", "MyValue");
* return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
* }
* </pre>
* Or, by using a builder accessible via static methods:
* <pre class="code">
* @RequestMapping("/handle")
* public ResponseEntity<String> handle() {
* URI location = ...;
* return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World");
* }
* </pre>
ResponseEntity和@ResponseStatus一起使用是无效的
@RequestMapping("/testResponseEntity")
public ResponseEntity<Map<String, String>> testResponseEntity(){
Map<String, String> map = ImmutableMap.of("key", "value");
return ResponseEntity.ok(map);
}
@RequestMapping("/testResponseEntityFaild")
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public ResponseEntity<Map<String, String>> testResponseEntityFaild(){
Map<String, String> map = ImmutableMap.of("key", "faild");
return ResponseEntity.ok(map);
}
@RequestMapping("/testResponseEntityFaild2")
public ResponseEntity<Map<String, String>> testResponseEntityFaild2(){
return ResponseEntity.badRequest().body(ImmutableMap.of("key", "faild"));
}
这三个处理程序方法返回的http状态码(http状态码)分别是200,200和400。
完整的代码的TestController
import com.google.common.collect.ImmutableMap;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* Created by saleson on 2017/5/5.
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/testResponseEntity")
public ResponseEntity<Map<String, String>> testResponseEntity(){
Map<String, String> map = ImmutableMap.of("key", "value");
return ResponseEntity.ok(map);
}
@RequestMapping("/testResponseEntityFaild")
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public ResponseEntity<Map<String, String>> testResponseEntityFaild(){
Map<String, String> map = ImmutableMap.of("key", "faild");
return ResponseEntity.ok(map);
}
@RequestMapping("/testResponseEntityFaild2")
public ResponseEntity<Map<String, String>> testResponseEntityFaild2(){
return ResponseEntity.badRequest().body(ImmutableMap.of("key", "faild"));
}
@RequestMapping("/testResponseBody")
@ResponseBody
public Map<String, String> testResponseBody(){
return ImmutableMap.of("key", "value");
}
@RequestMapping("/testResponseBodyFaild")
@ResponseBody
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public Map<String, String> testResponseBodyFaild(){
return ImmutableMap.of("key", "faild");
}
}
返回的http状态代码截图:
ResponseUtil的使用详情
1、Post请求
一般情况下,在非必须的情况下,使用Jquery实现post请求,而后台返回一般都需要手动封装ResponseUtil,和使用@ResponseBody注解来实现返回。
2、实例
1)前台代码:
返回实体中,第一个参数为返回的数据,相当于out.write(map.toString()),第二个参数是状态码,可以设置返回404,上述设置等价于返回200.
3、结果