0

I am creating simple web services using jersey.

Here all are working but i'm not able get data as xml format now i am using json format for display data for this json display i added jersey-media-moxy dependency, i don't know i need to add any xml dependency.

This is my pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <!-- uncomment this to get JSON support -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

    <dependency>
        <groupId>net.vz.mongodb.jackson</groupId>
        <artifactId>mongo-jackson-mapper</artifactId>
        <version>1.4.2</version>
    </dependency>
</dependencies>

This is my resouce

@Path("student")
public class StudentResource {

private static Map<Integer, Students> stud_data = new HashMap<Integer, Students>();

static{
    for(int i = 0; i < 10; i++){

        Students stu = new Students();
        int id = i+1;
        stu.setId(id);
        stu.setName("My Student "+id);
        stu.setAge(new Random().nextInt(15)+1);

        stud_data.put(id, stu);
    }
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Students> getall(){
    return new ArrayList<Students>(stud_data.values());
}
}

Please help me, how can i display the xml data like json.

Thanks in advance

selvam
  • 1,177
  • 4
  • 18
  • 40
  • `@Produces(MediaType.APPLICATION_XML)`? You can have more than one media type, just add them in between `{.., ..}` separated by comma. You also need to make sure to set the `Accept` header on the request to which type you want. – Paul Samsotha Sep 03 '15 at 07:44
  • i just give @Produces(MediaType.APPLICATION_XML), but display error message HTTP Status 500 - Internal Server Error – selvam Sep 03 '15 at 07:55
  • Add one of [these ExceptionMapper](http://stackoverflow.com/a/15185615/2587435). But use the generic type `Exception`, i.e. `ExceptionMapper`. My guess is it is a JAXB exception being thrown that is not handled by an exception mapper. Just print the stacktrace in the mapper. If you see a stacktrace, please post it. – Paul Samsotha Sep 03 '15 at 07:59
  • Also make sure the `Student` class is annotated with `@XmlRootElement` – Paul Samsotha Sep 03 '15 at 08:06

1 Answers1

3

According to Jersey documentation, you can either use Moxy or JAXB RI to convert objects to xml documents. If you want to use Moxy you need to do some further configurations which are defined here. But if you want to use reference implementation add jaxb dependency.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>2.16</version>
</dependency>

First you must annotate your classes with proper jaxb annotations. Here is an example (not minimal simple @XmlRootElement will also works).

@XmlRootElement(name = "student")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "user", propOrder = {
        "id",
        "name",
        "email"
})
public class StudentDto {

    @XmlElement(name = "id", nillable = false, required = false)
    private Long id;

    @XmlElement(name = "name", nillable = true, required = true)
    private String name;

    @XmlElement(name = "email", nillable = false, required = false)
    private String email;

    // GETTERS && SETTERS
}

Then you must annotate your services that they can both consume and produce XML documents. For example

@Path("student")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class StudentResource {

}

And finally your client must use proper headers, to tell server what kind of request it is doing and what kind of response it is waiting. Here is these headers

Content-Type: application/xml
Accept: application/xml

Which means in order to run getAllStudents service you need to execute something like (update: do not need Content-Type since there is no content in get. Thanks @peeskillet for comment) this.

curl -i \
    -H "Accept: application/xml" \
    -X GET \
    http://your_endpoint:8080/your_context/your_root_path/student
bhdrkn
  • 6,244
  • 5
  • 35
  • 42