9

My code actually contains one resource class and one BEan class it's return type is JSON.

Resource Class

@Path("/json")
public class JSONRetrive {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public JSONT getJson(){
        JSONT jsont = new JSONT();
        jsont.setJsont("inr");
        return jsont;
    }
}

/* Is there any Annotation will represent JSON. Do i need to use any other Annotation for this.*/

My Bean class

//@XmlRootElement
public class JSONT {
private String jsont;

public String getJsont() {
    return jsont;
}

public void setJsont(String jsont) {
    this.jsont = jsont;
}

}

If i uncomment @XmlRootElement it is working fine is there any other alternative solution for this

Community
  • 1
  • 1
NarayanaReddy
  • 119
  • 1
  • 1
  • 4
  • Yes no issue if i use @XmlRootElement and reponse also will be generated, But my Question is with out using JAXB can't we generate response(is there any alternative Annotation or process for JSON Object) – NarayanaReddy Mar 21 '13 at 13:52

3 Answers3

2

I got alternative solution for this @XmlRootElement, if we wanted to generate JSON response with out using @XmlRootElement we should go for Jersey Jackson library. Add the below given code in web.xml (DD file)

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

and Jackson API related jar's.

Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
NarayanaReddy
  • 119
  • 1
  • 1
  • 4
1

If you are using Jersey and Maven , then just go to your pom.xml and uncomment/add below code depending on your case

<!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
-->

After this, you can use @Produces(MediaType.APPLICATION_JSON) and you will get JSON response.

A W
  • 1,041
  • 11
  • 18
0

Maybe this will help? I don't see why is it a problem for you to use the annotation, at least in that case. Can you ellaborate more on the problem this causes to you?

Community
  • 1
  • 1
fcm
  • 6,305
  • 5
  • 24
  • 37