0

I want to create a custom http status code and want to return it with the response for specific use cases. I can not use the status codes like OK, ACCEPTED etc.

@RequestMapping(value = "/status/test", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Test> testStatus(@RequestBody Test test)
    throws Exception {
    // the below code is for status code 200

    HttpStatus httpStatus = HttpStatus.OK;

    // the above declaration declares the httpStatus with code 200 but I want to create
    // a custom code like 234
    
    return new ResponseEntity<Test>(test,httpStatus);

}
5
  • 1
    Take a look at this. Commented Dec 6, 2021 at 22:54
  • Guess that the status is stored as an object in the a Response Entity and you could use the rawStatus Method, which just takes an int. Would not do it, because you are breaking the contract and this could have strange side effects. Probably you could use a usual status code and return your custom code inside the body? Seems to be a better approach. Commented Dec 6, 2021 at 22:59
  • @AndrewS The link you shared throw an exception and I am not looking for it Commented Dec 7, 2021 at 9:41
  • @triplem can you write it down please how to achieve this. Commented Dec 7, 2021 at 9:42
  • You could try return new ResponseEntity<Test>(test, null, 4711); But, like already stated, you will definitly break the contract and it could also mean, that Spring will not return this correctly (haven't tested). Better would be something like an own Response-Object, which contains the Test-Object as well as your custom code, like already stated by frank in one of the above answers. This will not break any contract and gives you the freedom to do anything you like. Commented Dec 7, 2021 at 20:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

You could write your custom exception like this:

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class AccountNotFoundException extends RuntimeException {
    public AccountNotFoundException(Exception ex, int id) {
        super("Account with id=" + id + " not found", ex);
    }
}

Then throw the exception wherever you want. With @ResponseStatus you can give your exception it's own return code

1 Comment

I do not want to send an exception but a success status code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.