5

Here I have a Rest Controller

 @RequestMapping(value = "/mobileNumber", method = RequestMethod.POST, produces = {
                MediaType.APPLICATION_JSON_VALUE })
        public ResponseEntity<ResponseBack> sentResponse() {
    
            return new ResponseEntity<ResponseBack>(ResponseBack.LOGIN_SUCCESS, HttpStatus.ACCEPTED);
    
        }

My Enum Class

public enum ResponseBack {
    LOGIN_SUCCESS(0, " success"), LOGIN_FAILURE(1, " failure");

    private long id;
    private final String message;

    // Enum constructor
    ResponseBack(long id, String message) {
        this.id = id;
        this.message = message;
    }

    public long getId() {
        return id;
    }

    public String getMessage() {
        return message;
    }
}

When I get the response back from the controller I am getting it as

"LOGIN_SUCCESS"

What I require is

{
    "id": "0",
    "message": "success"   
}

How can I deserialize it to Json and send response, is there any annotation for it. Please help, thanks.

1 Answer 1

5

You must use JsonFormat annotation

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum ResponseBack {
...

So you tell that the Json representation of this enum will be the whole object. If you want a specific field to be returned (for example message field) you can annotate the method with JsonValue annotation

@JsonValue
public String getMessage() {
    return message;
}
Sign up to request clarification or add additional context in comments.

Comments

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.