我有一个Spring Boot REST应用程序。当路径不存在时,我需要返回400错误请求的状态码。考虑:
This is a good url
localhost:8080/mypath?name=test
Bad Path (return status 400)
localhost:8080/mybadpath?name=test
我如何使用Spring Boot做到这一点
通过实施,这对于spring boot-2.1.3有效ErrorController
@Controller
public class CustomErrorController implements ErrorController {
private static final String PATH = "/error";
@RequestMapping(value = PATH)
public ResponseEntity<String> error(WebRequest webRequest, HttpServletResponse response) {
return ResponseEntity.badRequest().body("");
}
@Override
public String getErrorPath() {
return PATH;
}
}
主要
@SpringBootApplication
public class BotMain {
public static void main(String[] args) {
SpringApplication.run(BotMain.class, args);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句