Java 如何通过springboot发送400/500/404响应错误
我以前使用的com.sun.httpserver有一个sendResponseheaders方法,我在springboot上找不到。无论如何都有办法做到这一点。基本上就是我想要的。如果我发送了一个带有用户名的json请求正文,那么将出现400响应错误您可以使用Java 如何通过springboot发送400/500/404响应错误,java,spring-boot,Java,Spring Boot,我以前使用的com.sun.httpserver有一个sendResponseheaders方法,我在springboot上找不到。无论如何都有办法做到这一点。基本上就是我想要的。如果我发送了一个带有用户名的json请求正文,那么将出现400响应错误您可以使用org.springframework.http.ResponseEntity @PutMapping("/createUser") public String createUser(@RequestBody User user) {
org.springframework.http.ResponseEntity
@PutMapping("/createUser")
public String createUser(@RequestBody User user) {
User thisValue = repository.findByUsername(user.getUsername());
if thisValue != null {
sendResponseHeaders(400, -1); // wont work
return "account exist";
}
sendResponseHeaders(200, -1); // wont work
return "reached here";
}
您可以使用
org.springframework.http.ResponseEntity
@PutMapping("/createUser")
public String createUser(@RequestBody User user) {
User thisValue = repository.findByUsername(user.getUsername());
if thisValue != null {
sendResponseHeaders(400, -1); // wont work
return "account exist";
}
sendResponseHeaders(200, -1); // wont work
return "reached here";
}
在普通控制器中:
@PutMapping("/createUser")
public ResponseEntity createUser(@RequestBody User user) {
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
@PutMapping("/createUser")
public ModelAndView createUser(@RequestBody User user) {
ModelAndView mv= new ModelAndView("reached-here");
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
model.setStatus(HttpStatus.BAD_REQUEST);
model.setValueName("account-exist");
}else{
model.setStatus(HttpStatus.OK);
}
return mv;
}
在REST控制器中:
@PutMapping("/createUser")
public ResponseEntity createUser(@RequestBody User user) {
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
@PutMapping("/createUser")
public ModelAndView createUser(@RequestBody User user) {
ModelAndView mv= new ModelAndView("reached-here");
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
model.setStatus(HttpStatus.BAD_REQUEST);
model.setValueName("account-exist");
}else{
model.setStatus(HttpStatus.OK);
}
return mv;
}
@PutMapping(“/createUser”)
public ResponseEntity createUser(@RequestBody User){
User thisValue=repository.findByUsername(User.getUsername());
if(thisValue!=null){
返回新的响应属性(“帐户存在”,HttpStatus.BAD_请求);
}否则{
返回新的响应状态(“到达此处”,HttpStatus.OK);
}
}
在普通控制器中:
@PutMapping("/createUser")
public ResponseEntity createUser(@RequestBody User user) {
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
@PutMapping("/createUser")
public ModelAndView createUser(@RequestBody User user) {
ModelAndView mv= new ModelAndView("reached-here");
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
model.setStatus(HttpStatus.BAD_REQUEST);
model.setValueName("account-exist");
}else{
model.setStatus(HttpStatus.OK);
}
return mv;
}
在REST控制器中:
@PutMapping("/createUser")
public ResponseEntity createUser(@RequestBody User user) {
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
@PutMapping("/createUser")
public ModelAndView createUser(@RequestBody User user) {
ModelAndView mv= new ModelAndView("reached-here");
User thisValue = repository.findByUsername(user.getUsername());
if (thisValue != null) {
model.setStatus(HttpStatus.BAD_REQUEST);
model.setValueName("account-exist");
}else{
model.setStatus(HttpStatus.OK);
}
return mv;
}
@PutMapping(“/createUser”)
public ResponseEntity createUser(@RequestBody User){
User thisValue=repository.findByUsername(User.getUsername());
if(thisValue!=null){
返回新的响应属性(“帐户存在”,HttpStatus.BAD_请求);
}否则{
返回新的响应状态(“到达此处”,HttpStatus.OK);
}
}
除了返回显式的响应属性
,您还可以抛出一个异常(或允许异常转义),该异常在类级别用@ResponseStatus
注释。默认的Spring MVC配置将拦截来自控制器方法的任何异常,如果没有注释或指定状态,则将返回HTTP 500(一般“服务器错误”)。除了返回显式的响应属性
,还可以抛出异常(或允许异常转义)在类级别用@ResponseStatus
注释。默认的Spring MVC配置将截获来自控制器方法的任何异常,如果没有注释,将返回HTTP 500(一般“服务器错误”),如果有注释,将返回指定状态。然后呢?比如说什么是方法,然后呢?喜欢什么方法