Deadpool
Deadpool

Reputation: 94

JAX-RS Restful POST without a fixed JSON body

I have a rest service.

@POST
@Path("/feedback")
@Consumes(MediaType.APPLICATION_JSON)
public void saveFeedback(FeedbackRequest feedback){

and my FeedbackRequest class has a JSONObject property.

public class FeedbackRequest {

private String message;
private JSONObject payload;
private String type;

The reason i put JSONObject in it is i can get any object with different attributes. But when i fire my api, I am getting bad request exception when processing 'payload'. My question is how can i process a dynamic json body data.

Upvotes: 1

Views: 789

Answers (1)

cassiomolin
cassiomolin

Reputation: 131087

My question is how can I process a dynamic JSON body data.

With Jackson, you could do the following:

public class FeedbackRequest {

    private String type;
    private String message;
    private Map<String, Object> payload;

    // Getters and setters
}

Alternatively you can use JsonNode instead of Map<K, V>.

Upvotes: 2

Related Questions