James111 James111 - 1 year ago 62
Java Question

Spring return custom message not returning as expected

I've got the following error classes:

private static class GenericError extends Error {
private int code;
private String message;
private Date timestamp;

public GenericError(String message, int code, String message1) {
this.code = code;
this.message = message1;
this.timestamp = new Date();

public int getCode() {
return code;

public String getMessage() {
return message;

public Date getTimestamp() {
return timestamp;
private static class ConfirmResponse extends GenericError {
private String confirmMessage;
private String instructions;

public ConfirmResponse(String message, int code, String message1, String confirmMessage, String instructions) {
super(message, code, message1);
this.confirmMessage = confirmMessage;
this.instructions = instructions;

public String getConfirmMessage() {
return confirmMessage;

public String getInstructions() {
return instructions;

I'm using it like so:

@RequestMapping(value="login", method=RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE })
FbLoginResponse login(@RequestBody Body body) throws IOException, InvalidKeySpecException,
NoSuchAlgorithmException, ConfirmLogin, ClassNotFoundException, ConfirmResponse {


ConfirmResponse r = new ConfirmResponse("Error", 111, "An account already exists under this email",
"Please confirm your account",
"Log in with your prev account");

throw r;

But the response I'm getting is incorrect:

"timestamp": 1467604509746,
"status": 500,
"error": "Internal Server Error",
"exception": "com.userapi.spring.controller.UserController$ConfirmResponse",
"message": "An account already exists under this email",
"path": "/user/login/fac


I also tried extending Exception which produced the same message.


I want to be able to return multiple different Class objects to the client. So if there was an error I could return a

new Error("Error occured")

and if I needed to return a custom message I could do

return new CustomMessage("Check your Email!);

I cannot do this when I define the return type of the controller e.g

LoginResp login(@RequestBody UserFacebookLoginContext body)

... I can only return LoginResp, I cannot return ErrorResponse, or CustomResponse

Basically what I want to be able todo is return multiple types of classes e.g

AnyResp login(@RequestBody UserFacebookLoginContext body)
if error ... return new ErrorResponse("something went wrong");

if requires-custom ... return new CustomResponse ("Please check your email");

Answer Source

i'll leave you an example of how i handle diffent objects to return with ResponseEntity

@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<?> getAll() {
    List<Transferencia> list = transferenciaDAO.getAll();
    if (!list.isEmpty()) {
        return new ResponseEntity<>(list, HttpStatus.OK);
    } else {//here you can return any type of object doing your respective control of field.
        JsonResponse msj = new JsonResponse("Error", "List empty");
        return new ResponseEntity<>(msj, HttpStatus.NOT_FOUND);

also if you don't want to return any error. you can just return something similar to this:

return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download