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

Sunday, January 20, 2013

Spring org.springframework.beans.BeanInstantiationException - Solved

Links to this post
I was working on JAX-RS and encountered with BeanInstantiationException.

Exception :
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.anuj.services.CountryServiceImpl]: Constructor threw exception; nested exception is com.anuj.common.errors.InternalServerException: Internal Server Error Occured
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:983)
    ... 41 more
Caused by: com.anuj.common.errors.InternalServerException: Internal Server Error Occured
    at com.anuj.services.CountryServiceImpl.initialize(CountryServiceImpl.java:160)
    at com.anuj.services.CountryServiceImpl.(CountryServiceImpl.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 43 more


Friday, April 27, 2012

JAX-RS Handling XML using JAXB Annotation

Links to this post
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 :
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@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;
}

}

File Download in JAX-RS

Links to this post
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();
}
}

@HeaderParam Implementation in JAX-RS

Links to this post
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.

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserService {

@GET
@Path("/get")
public Response addUser(@HeaderParam("user-agent") String userAgent) {
  return Response.status(200)
.entity("addUser is called, userAgent : " + userAgent)
.build();
}
}

Access via URI pattern “/users/get“, with FireFox, see following result :
addUser is called, userAgent : Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0

@MatrixParam Implementation in JAX-RS

Links to this post
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();

 }
}