I am learning to create a rest api. Here is my MessageResource class:
@Path("/message")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class MessageResource {
private Map messages = Database.getMessages();
@GET
@Path("/all")
public List allMessages() {
return new ArrayList(messages.values());
}
//... some other stuff
}
Message class:
@XmlRootElement
public class Message {
private Integer id;
private String message;
private String author;
public Message() {}
public Message(Integer id, String message, String author) {
this.id = id;
this.message = message;
this.author = author;
}
public boolean isValidToAdd() {
if(Database.getMessages().containsKey(id) || id < 0) return false;
if(message.isEmpty()) return false;
if(author.isEmpty()) return false;
return true;
}
//getters and setters for "id", "message" and "author"
}
The problem is that the response generated by MessageResource.allMessages() contains a field validToAdd when the Accept header is application/json but does not show when Accept header is application/xml or text/xml. I have tried adding @XmlTransient and @JsonbTransient to Message.isValidToAdd() but it still shows up. Any suggestion on how to remove validToAdd key from json response?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)