Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HttpServletRequest in Jax Rs / Appfuse application?

I created a basic application shell with AppFuse, and followed the AppFuse tutorial to create a a simple RESTful service with Jax-RS. That works just fine. A call to http://localhost:8080/services/api/persons returns a collection of Person objects as a Json formatted string with the right data.

I now would like to access the ServletRequest and ServletResponse objects from within a RESTful service exposed by Appfuse (to use another library that requires these objects).

I think that should be doable by adding an @Context annotation, e.g. following this StackOverflow post and this forum post.

But if I add the @Context tag (see below), it compiles fine but throws an Exception when the server is restarted (attached at bottom).

Here's the declaration of the @WebService:

@WebService
@Path("/persons")
public interface PersonManager extends GenericManager<Person, Long> {
    @Path("/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    List<Person> read();
    ...
}

And here's the implementation class where I think I would call the @Context annotation:

@Service("personManager") 
public class PersonManagerImpl extends GenericManagerImpl<Person, Long> implements PersonManager { 
    PersonDao personDao; 
    @Context ServletRequest request; // Exception thrown on launch if this is present 
    @Context ServletContext context;  // Exception thrown on launch of this is present 
    ... 
    } 

Hopefully I'm missing something simple, either something to include to make it work, or to realize that getting the ServletRequest isn't supposed to be possible because.... Any clues would be welcome.

Am running this on Tomcat in IntelliJ.

=== EXCEPTION STACK TRACE (Truncated) ===

Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: 
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'serviceBeans' threw exception; nested exception is java.lang.RuntimeException: java.lang.NullPointerException 
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102) 
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58) 
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358) 
        ... 37 more 
like image 453
prototype Avatar asked Mar 08 '12 02:03

prototype


People also ask

How do I pass HttpServletRequest in Java?

Pass it to the constructor: public class XmlParser{ final private HttpServletRequest request; public XmlParser(HttpServletRequest request) { this. request = request; } // use it in othe methods... }

What is the difference between ServletRequest and HttpServletRequest?

ServletRequest provides basic setter and getter methods for requesting a Servlet, but it doesn't specify how to communicate. HttpServletRequest extends the Interface with getters for HTTP-communication (which is of course the most common way for communicating since Servlets mostly generate HTML).

What is @context HttpServletRequest?

The @Context annotation is used to inject the HttpServletRequest instance for the current request. Its methods give access to detailed information about the request.


2 Answers

Try injecting the HttpServletRequest and HttpServletContext directly:

@Context private HttpServletRequest servletRequest; @Context private HttpServletContext servletContext; 
like image 185
Perception Avatar answered Sep 19 '22 23:09

Perception


Adding it to the method signature worked. I imagine it's because the request and response objects don't yet exist when the class is instantiated, but does when called by the brower.

@Path("/") @GET @Produces(MediaType.APPLICATION_JSON) List<Person> read( @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse) { } 
like image 33
prototype Avatar answered Sep 20 '22 23:09

prototype