在 Jax Rs/Appfuse 应用程序中获取 HttpServletRequest? [英] Get HttpServletRequest in Jax Rs / Appfuse application?

查看:21
本文介绍了在 Jax Rs/Appfuse 应用程序中获取 HttpServletRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 

解决方案

Try injecting the HttpServletRequest and HttpServletContext directly:

@Context private HttpServletRequest servletRequest;
@Context private HttpServletContext servletContext;

这篇关于在 Jax Rs/Appfuse 应用程序中获取 HttpServletRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆