SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

четверг, 31 октября 2013 г.

Apache CXF and AOP (AspectJ) MessageContext problem (Spring, Maven)

Hi,
as AOP is the cool way to implement logging i've decided to add it to my web-service. Initial steps was very easy, google helps to find step by step guides with code examples for Spring web-app. I was happy with my AOP powered web-service until the first Exception. NullPointerException raises when i wanted to get HttpServletRequest from MessageContext that was injected as @Context.


@Context private MessageContext context;



So, we need some information from context in service and this information isnt available. We can get this information in interceptors and somehow send it to the service or work on making the context available in service. One of the ways is to change the scope of cxf web-service bea to request and work on it (yo ucan find information in CXF official documentation). But the best way i found is to add interface to explicitly adds the context. Here the soution:
Step 1: Create an interface:

import javax.ws.rs.core.Context; 
import org.apache.cxf.jaxrs.ext.MessageContext;
 /** * * @author Vusal J. Khalilov */
 public interface AopContextInterface { 
 @Context
 void setMessageContext(MessageContext ctx);
 }

Implement this interface in your web-service class and override this method:

@Override
 public void setMessageContext(MessageContext ctx)
 {
 this.context=ctx; 
 }

Another main point is the version of CXF and Jackson. I used version 2.2.2 for CXF and it doesnt works for me. I upgraded it to 2.7.7 and made a minor changes in cxf-beans.xml Cxf configuration file:
Delete this lines:
And add the following lines in order to avoid the error: "No message body writer has been found for response class"


<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" id="JsonProvider">    
    <jaxrs:server address="/***" id="***">
        <jaxrs:servicebeans>
            <ref bean="***">
        </ref></jaxrs:servicebeans>
        <jaxrs:providers>
            <ref bean="***">
            <ref bean="JsonProvider">            
        </ref></ref></jaxrs:providers>                
    </jaxrs:server>
</bean>


Thats all, and before i say good bye i want to share a piece of pom.xml i used to implement AOP, context injecting with CXF and CXF upgrade.



        <dependency>
            <groupid>cglib</groupid>
            <artifactid>cglib</artifactid>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aop</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aspects</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.aspectj</groupid>
            <artifactid>aspectjweaver</artifactid>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupid>aspectj</groupid>
            <artifactid>aspectjrt</artifactid>
            <version>1.5.4</version>
        </dependency>
        <dependency>
            <groupid>com.fasterxml.jackson.core</groupid>
            <artifactid>jackson-databind</artifactid>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupid>com.fasterxml.jackson.jaxrs</groupid>
            <artifactid>jackson-jaxrs-json-provider</artifactid>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupid>org.apache.cxf</groupid>
            <artifactid>cxf-bundle</artifactid>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupid>org.apache.cxf</groupid>
            <artifactid>cxf-bundle-jaxrs</artifactid>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupid>org.apache.cxf</groupid>
            <artifactid>cxf-rt-frontend-jaxrs</artifactid>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupid>org.apache.cxf</groupid>
            <artifactid>cxf-rt-transports-http</artifactid>
            <version>${cxf.version}</version>
        </dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
        <dependency>
            <groupid>org.apache.cxf</groupid>
            <artifactid>cxf-rt-transports-http-jetty</artifactid>
            <version>${cxf.version}</version>
        </dependency>

Комментариев нет:

Отправить комментарий