- Java response bad request
- Nested Class Summary
- Enum Constant Summary
- Method Summary
- Methods inherited from class java.lang.Enum
- Methods inherited from class java.lang.Object
- Enum Constant Detail
- OK
- CREATED
- ACCEPTED
- NO_CONTENT
- RESET_CONTENT
- PARTIAL_CONTENT
- MOVED_PERMANENTLY
- FOUND
- SEE_OTHER
- NOT_MODIFIED
- USE_PROXY
- TEMPORARY_REDIRECT
- BAD_REQUEST
- UNAUTHORIZED
- PAYMENT_REQUIRED
- FORBIDDEN
- NOT_FOUND
- METHOD_NOT_ALLOWED
- NOT_ACCEPTABLE
- PROXY_AUTHENTICATION_REQUIRED
- REQUEST_TIMEOUT
- CONFLICT
- GONE
- LENGTH_REQUIRED
- PRECONDITION_FAILED
- REQUEST_ENTITY_TOO_LARGE
- REQUEST_URI_TOO_LONG
- UNSUPPORTED_MEDIA_TYPE
- REQUESTED_RANGE_NOT_SATISFIABLE
- EXPECTATION_FAILED
- INTERNAL_SERVER_ERROR
- NOT_IMPLEMENTED
- BAD_GATEWAY
- SERVICE_UNAVAILABLE
- GATEWAY_TIMEOUT
- HTTP_VERSION_NOT_SUPPORTED
- Method Detail
- values
- valueOf
- getFamily
- getStatusCode
- getReasonPhrase
- toString
- fromStatusCode
- Spring: Best Ways to return HTTP 400 Bad Request
Java response bad request
Commonly used status codes defined by HTTP, see HTTP/1.1 documentation for the complete list. Additional status codes can be added by applications by creating an implementation of Response.StatusType .
Nested Class Summary
Enum Constant Summary
Method Summary
Methods inherited from class java.lang.Enum
Methods inherited from class java.lang.Object
Enum Constant Detail
OK
CREATED
ACCEPTED
NO_CONTENT
RESET_CONTENT
PARTIAL_CONTENT
MOVED_PERMANENTLY
FOUND
SEE_OTHER
NOT_MODIFIED
USE_PROXY
TEMPORARY_REDIRECT
BAD_REQUEST
UNAUTHORIZED
PAYMENT_REQUIRED
FORBIDDEN
NOT_FOUND
METHOD_NOT_ALLOWED
NOT_ACCEPTABLE
PROXY_AUTHENTICATION_REQUIRED
public static final Response.Status PROXY_AUTHENTICATION_REQUIRED
REQUEST_TIMEOUT
CONFLICT
GONE
LENGTH_REQUIRED
PRECONDITION_FAILED
REQUEST_ENTITY_TOO_LARGE
REQUEST_URI_TOO_LONG
UNSUPPORTED_MEDIA_TYPE
REQUESTED_RANGE_NOT_SATISFIABLE
public static final Response.Status REQUESTED_RANGE_NOT_SATISFIABLE
EXPECTATION_FAILED
INTERNAL_SERVER_ERROR
NOT_IMPLEMENTED
BAD_GATEWAY
SERVICE_UNAVAILABLE
GATEWAY_TIMEOUT
HTTP_VERSION_NOT_SUPPORTED
public static final Response.Status HTTP_VERSION_NOT_SUPPORTED
Method Detail
values
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for (Response.Status c : Response.Status.values()) System.out.println(c);
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
getFamily
getStatusCode
getReasonPhrase
toString
fromStatusCode
public static Response.Status fromStatusCode(int statusCode)
Spring: Best Ways to return HTTP 400 Bad Request
In HTTP requests, a 400 Bad Request response status code indicates that the server cannot process the request due to a client error (eg. malformed request syntax, invalid request message framing, or deceptive request routing).
In Spring controller in most cases, the Spring framework handles most of the syntax and payload validation and responds 400 bad requests. But many times we need to do more validation of the request payload or during processing, our code determines the wrong parameters and returns a Bad request.
To do this first of all you need to change the return type to ResponseEntity<> , and then you can use the below for return 400 status:
@ApiOperation(value = "Add new email and company for 2F authentication",response = TotpResource.class) @PostMapping(consumes = "application/json", produces = "application/json") public ResponseEntity addNew(@Valid @RequestBody TotpResource totpResource) < if(Strings.isStringEmpty(totpResource.getEmailId())< return new ResponseEntity<>(HttpStatus.BAD_REQUEST); > totpResource = this.totpService.addNew(totpResource); return ResponseEntity.created(URI.create("/" + totpResource.getEmailId())).body(totpResource); >
After Spring 4.1 there are helper methods in ResponseEntity util class which could be used as:
@ApiOperation(value = "Add new email and company for 2F authentication",response = TotpResource.class) @PostMapping(consumes = "application/json", produces = "application/json") public ResponseEntity addNew(@Valid @RequestBody TotpResource totpResource) < if(Strings.isStringEmpty(totpResource.getEmailId())< return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Arrays.asList(new String[])); > totpResource = this.totpService.addNew(totpResource); return ResponseEntity.created(URI.create("/" + totpResource.getEmailId())).body(totpResource); >
It is always good to return a detailed error message in a bad request response body, that will be used to understand the problem by the client.
Another way but not the best ways if you don’t want to change the the return type and other things then you just add HttpServletResponse response in the existing method, if not present already, and set the response status as given below.
@RequestMapping(value = "/matches/", produces = "application/json") @ResponseBody public String matchMethod(@PathVariable String matchId, @RequestBody String body, HttpServletRequest request, HttpServletResponse response) < String json = matchService.getMatchJson(matchId); if (json == null) < response.setStatus( HttpServletResponse.SC_BAD_REQUEST ); >return json; >