29

AppFuse で基本的なアプリケーション シェルを作成し、 AppFuse のチュートリアルに従って、Jax-RS で単純な RESTful サービスを作成しました。それはうまくいきます。を呼び出すhttp://localhost:8080/services/api/personsと、適切なデータを含む JSON 形式の文字列として Person オブジェクトのコレクションが返されます。

Appfuse によって公開された RESTful サービス内から ServletRequestおよびオブジェクトにアクセスしたいと考えています(これらのオブジェクトを必要とする別のライブラリを使用するため)。ServletResponse

@Context アノテーションを追加することでそれが可能になると思います。たとえば、このStackOverflow投稿と このフォーラムの投稿に従ってください。

しかし、@Context タグ (以下を参照) を追加すると、問題なくコンパイルされますが、サーバーの再起動時に例外がスローされます (下部に添付)。

の宣言は次の@WebServiceとおりです。

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

@Contextそして、これが私がアノテーションを呼び出すと思う実装クラスです:

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

うまくいけば、それを機能させるために含めるもの、またはServletRequestを取得することは不可能であることを理解するために、単純なものが欠けていることを願っています....手がかりは大歓迎です。

これを IntelliJ の Tomcat で実行しています。

=== 例外スタック トレース (切り捨て) ===

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 
4

2 に答える 2

44

HttpServletRequestand をHttpServletContext直接注入してみてください。

@Context private HttpServletRequest servletRequest;
@Context private HttpServletContext servletContext;
于 2012-03-08T04:03:45.730 に答える
30

メソッド署名に追加するとうまくいきました。クラスがインスタンス化されたときにリクエストオブジェクトとレスポンスオブジェクトがまだ存在していないが、ブラウザによって呼び出されたときに存在するためだと思います。

@Path("/")
@GET
@Produces(MediaType.APPLICATION_JSON)
List<Person> read( @Context HttpServletRequest httpServletRequest, @Context HttpServletResponse httpServletResponse) { }
于 2012-03-09T01:38:24.297 に答える