0

I have a DB interface which returns me HashMap. I'm writing a JAX-RS REST interface to retrieve data from this DB and display in browser as either XML or JSON.

@XmlRootELement tag takes care of POJO to XML/JSON conversion.

How should I tackle Map to XML/JSON conversion?

Any help is appreciated!

1 Answer 1

1

Consider following Rest Interface. We define Annotation "Produces" which can generate different format type output. In your case APPLICATION_JSON will generate JSON and APPLICATION_XML will generate XML format. So decide accordingly.

package : javax.ws.rs.PathParam; @GET @Path("/helloRest") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public MyPojo getHello(); 

You can control how XML format is generated using Annotation from package javax.xml.bind.annotation. This will help you to control your POJO objects names and other validations if needed

@XmlRootElement(name="MyHello") @XmlAccessorType(XmlAccessType.FIELD) public class MyPojo { @XmlElement(name="id") private int id; @NotNull @Size(min = 1, max = 10, message = "Mandatory Field") @FormParam("code") private String code; @NotNull @Size(min = 1, max = 50, message = "Mandatory Field") @FormParam("name") private String name; @FormParam("status") private String status; JSON Format: {"id":48,"code":"dfgfdgd","name":"aaaaaaaaa","status":"dfgfdgdd"} XML Format: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <MyHello><id>168</id> <code>AS3</code> <name>gfhfgh</name> <status>A</status> </MyHello> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.