in Education by
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)

1 Answer

0 votes
by
it's better to separate message validator into another class. but if you want to have it in the message class you can consider a filed for it for example public boolean validToAdd; now if you want to jsonb ignore it you can mark it as @JsonbTransient public boolean validToAdd; but xml binding does not ignore that and you can add @XmlTransient too. or make isValidToAdd a private method by that it can be only invoked from inside the class. there is another way by playing with jsonb and create a custom setting for it(JsonbConfig). its better to separate messagevalidator as a class

Related questions

0 votes
    Which of the below does the task of binding the parameters passed to the HTTP method to HTTP Header while using ... ? 1. @PathParam 2. @QueryParam 3. @HeaderParam 4. @PathVariable...
asked Jun 24, 2021 in Technology by JackTerrance
0 votes
    What do you understand by JAX-RS?...
asked Jun 22, 2021 in Technology by JackTerrance
0 votes
0 votes
    In the expression of current in the R-L circuit the transient part is? (a) R/V (b) (V/R)(-exp⁡(( ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 17, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What is the difference between JAX--WS and JAX-RPC?...
asked Jun 6, 2021 in Technology by JackTerrance
0 votes
    Write· a program to read one of the hex digits A-I’, and...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    A piggy bank contains hundred 50p coins, fifty Re. 1 coins, twenty Rs. 2 coins and ten Rs. 5 coins. If it is ... be a Rs. 5 coin ? Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
0 votes
    In a game, the entry fee is of Rs.5. The game consists of a tossing a coin 3 times. If one or two heads ... ) just gets her entry fee. Select the correct answer from above options...
asked Nov 21, 2021 in Education by JackTerrance
...