Showing posts with label JAX-RS. Show all posts
Showing posts with label JAX-RS. Show all posts

JAX-RS Handling XML using JAXB Annotation

In this tutorial, I will show you how to create an “user” object, convert it into XML file,
and return it back to the client.

Create an object, annotate with JAXB annotation to support XML file conversion.

POJO Class with JAXB annotation :
@XmlRootElement(name = "user")

public class User {

 String username;
 String password;
 int pin;

 @XmlElement
 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 @XmlElement
 public String getPassword() {
  return password;
 }
public void setPassword(String password) {
  this.password = password;
 }

 @XmlAttribute
 public int getPin() {
  return pin;
 }

 public void setPin(int pin) {
  this.pin = pin;
 }
}


Restful Service for XML in JAX-RS :
To return a XML file, annotate the service method with @Produces("application/xml").
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/xml/user")
public class XMLService {

 @GET
 @Path("/get")
 @Produces("application/xml")
 public User getUserInXML() {

  User user = new User();
  user.setUsername("anuj");
  user.setPassword("password");
  user.setPin(123456);

  return user;

 }

}

File Download in JAX-RS

In JAX-RS, for user to download a file, annotate the method with @Produces("text/plain") :

Put @Produces(“text/plain”) on service method, with a Response return type. It means the output is a text file.
Set “Content-Disposition” in Response header to tell browser pop up a download box for user to download.

Download File in JAX-RS :

See a full example to download a text file in JAX-RS.

import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;


@Path("/file")
public class FileService {

 private static final String FILE_PATH = "c:\\file.log";

 @GET
 @Path("/get")
 @Produces("text/plain")
 public Response getFile() {
      File file = new File(FILE_PATH);

      ResponseBuilder response = Response.ok((Object) file);
      response.header("Content-Disposition","attachment; filename=\"file_from_server.log\"");
      return response.build();
  }

}


File Type :
txt : @Produces("text/plain")
image  : @Produces("image/png")
pdf : @Produces("application/pdf")
excel : @Produces("application/vnd.ms-excel")

@HeaderParam Implementation in JAX-RS

There are 2 ways to get HTTP request header in JAX-RS :
Inject directly with @HeaderParam
Pragmatically via @Context

Implementation using @HeaderParam :

In this example, it gets the browser “user-agent” from request header.

@MatrixParam Implementation in JAX-RS

In this tutorial , i will show usage of @MatrixParam. Matrix parameters are a set of “name=value” in URI path, for example, /books/2012;author=anuj
In above URI, the matrix parameter is “author=anuj“, separate by a semi colon “;“.


@MatrixParam example

import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/books")
public class BookService {

 @GET
 @Path("{year}")
 public Response getBooks(@PathParam("year") String year,
   @MatrixParam("author") String author,
   @MatrixParam("country") String country) {

  return Response
   .status(200)
   .entity("getBooks is called, year : " + year
    + ", author : " + author + ", country : " + country)
   .build();
 }
}

Results :
1. URI Pattern : “/books/2012/”
getBooks is called, year : 2012, author : null, country : null
2. URI Pattern : “/books/2012;author=anuj”
getBooks is called, year : 2012, author : anuj, country : null
3. URI Pattern : “/books/2012;author=anuj;country=usa”
getBooks is called, year : 2012, author : anuj, country : usa
4. URI Pattern : “/books/2012;country=usa;author=anuj”
getBooks is called, year : 2012, author : anuj, country : usa