How to Create Custom Http Status Codes

Create and Return a custom Http response code in Java Spring Boot

you could take a look at the link provided by Andrew S in the comment.

another option is encapsulate the response into a custom java bean via @RestControllerAdvice, this custom java bean looks like

public class ResultVO<T> implements Serializable{

private int code;
private String msg;
private T data;

then you could set any custom code in it.

What HTTP status code should I use for custom errors?

Can I use 400 BadRequest for all those errors?

Most certainly.

This used to be a bit questionable, because RFC 2616 defined 400 Bad Request as:

The request could not be understood by the server due to malformed
syntax. The client SHOULD NOT repeat the request without
modifications.

However, there often wasn't a more applicable better status, so it was often used as the best-fit.

This has changed with RFC 7231 obsoleting RFC 2616 and giving a broader definition to 400:

The 400 (Bad Request) status code indicates that the server cannot or
will not process the request due to something that is perceived to be
a client error (e.g., malformed request syntax, invalid request
message framing, or deceptive request routing).

Because "something that is perceived to be a client error" covers a multitude of sins, it's now more explicitly applicable.

Of course, if another 4xx code does match better (e.g. 404 for a request that relates to something that doesn't exist [an ID in the message doesn't find a match]), then it is the better option.

Custom Http Status Code in Spring

I'm not aware of a way to do this with @ResponseStatus.

One way to solve is this issue is with @RestControllerAdvice.
This will allow you to customize how you return your Exception.

@RestControllerAdvice
public class WebRestControllerAdvice {

@ExceptionHandler(ExternalDependencyException.class)
public String handleGitlabException(ExternalDependencyException ex, HttpServletResponse response) {
try {
response.sendError(460);
} catch (Exception e) {
e.printStackTrace();
}
return ex.getMessage();
}
}

Creating custom status codes

There are quite a lot of existing codes, so usually you won't need to invent new ones. You might, though, but when you do, make sure to use the right ranges.

For instance, the 200+ range indicates success of any kind, 300+ is a redirect, 400+ range is a client error (bad url format, target not found), and 500+ is a server error.

By following these guidelines, you can use all kinds of clients to communicate to your api. A browser should normally display the results of any 2XX status code, and treat it as a succesful request, even if the particular XX is unknown to it.

For a rather complete list of status codes and their normal meaning, as well as a general description of each of the ranges, see this:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

599 is the highest assigned status code. I wouldn't know if it's allowed to use the 600+ range, but I cannot find anything about it. I cannot imagine that your status wouldn't fit in any of the existing categories, though.

REST:
Aside from the return code, you can (should?) also have a look at the different methods. You probably know GET (sending data in url) and POST (sending extra data with the request), because they are commonly used. But you also got PUT and DELETE, which are especially useful for an api like this. If you search for terms like Restfull API, you should get plenty of documentation about this subject. Also, see this W3 link for an overview of request methods.

Bringing it together:
For creating a customer through the api, you could send a PUT request with the data of the new customer. The api can return 201 Created with an ID (or slug) for the customer. If you want to delete the customer, send a DELETE request and return 204 No content, since you just need to confirm that the delete succeeded, without sending any content.

Custom HTTP status code

If you want to change the default content-type, status or headers, the method should return a net.codestory.http.payload.Payload.

Here's what you should write:

@Post("/")
public Payload login(LoginRequest loginRequest) {
if(!loginRequest.getPassword().equals("helloworld")) {
return new Payload("ERROR").withCode(HttpStatus.UNAUTHORIZED);
}

return new Payload("SUCCESS").withCode(HttpStatus.CREATED);
}


Related Topics



Leave a reply



Submit