Cxf-interceptor get call Method

Source: Internet
Author: User

I did not expect such a thing.

At first, I only wanted to use Interceptor to record WebService call logs, but was asked to display them on the page.

It is easy to display, but it only shows the WebService address, which cannot be understood by users.

So I can take the URL and the specific description as a key value, but where is the key value better configured?

File? DB? I don't want to configure the key value elsewhere every time I add a method. Writing annotations may be a good method.

 

This requires me to obtain the corresponding method object after the method is called.

Of course, there are many ways to achieve this effect. Here we mainly talk about how to obtain it in cxf interceptor.

 

By the way, I configure spring + cxf in Web applications. The interceptor simply inherits jaxrsin/outinterceptor.

For example, I have a method like this:

 

@ Get
@ Path ("/Hello ")
@ Consumes ({"*/*"})
Public String Hello (){

Return "Hello alvez! ";

}

I need to record the method name after the call ends, but it cannot be "hello", but "hello ".

 

Define an annotation and add the following to the method:

@ Target ({elementtype. Method })
@ Retention (retentionpolicy. runtime)
Public @ interface description {
Public String Value ();
}

 

@ Get @ path ("/Hello") @ consumes ({"*/*"}) @ description ("Haro") Public String Hello () {return "Hello alvez! ";}

 

Well, the next step is to get the call information from the interceptor.

Ininterceptor or outinterceptor?

No matter which one, when handlemessage is rewritten, we naturally want to get it from the message object.

Of course, if you find that there is no message type in the list of currently used method parameters, you may do this.

Message message =  JAXRSUtils.getCurrentMessage();

 

Because the jaxin/outinterceptor used in this example inherits abstractphaseinterceptor <message>, We can naturally get the message object.

public class JAXRSInInterceptor extends AbstractPhaseInterceptor<Message> {      //..      }

 

However, a message contains a lot of content, such as exchange, destination, attachment, and interceptorchain. If you do not understand what they represent, you cannot find them.

Maybe I want to use org. Apache. cxf. Service. invoker. methoddispatcher (Org. Apache. cxf. frontend. methoddispatcher in 2.6.x)

The reason is that he has a method to get the java. Lang. Reflect. Method object through the bindingoperationinfo object:

public interface MethodDispatcher {    Method getMethod(BindingOperationInfo op);        //......}

 

In this way, I can use the following in the Interceptor:

Exchange exchange = message.getExchange();BindingOperationInfo bindingOperationInfo = exchange.getBindingOperationInfo();SimpleMethodDispatcher methodDispatcher = new SimpleMethodDispatcher();Method method = methodDispatcher.getMethod(bindingOperationInfo);

 

But unfortunately, which bindingoperationinfo comes from exchange?

What is in exchange? Now that he is a subclass of map, I can look at his keyset and find the message. getexchange (). keyset () in outinterceptor is as follows:

Org. Apache. cxf. Rest. Message
Org. Apache. cxf. Resource. Operation. Name
Org. Apache. cxf. Interceptor. loggingmessage. ID
Org. Apache. cxf. endpoint. conduitselector
Org. Apache. cxf. jaxrs. model. operationresourceinfo
Org. Apache. cxf. Service. Object. Last
Org. Apache. cxf. endpoint. endpoint
Org. Apache. cxf. Service. Service
Root. Resource. Class
Service. Root. Provider
Service. Root. Instance
Org. Apache. cxf. Bus
Org. Apache. cxf. Binding. Binding
Accept
Content-Type

 

We can see that operationresourceinfo can be obtained. This parameter is also included in the default method of the request/responsehandler interface, but these two interfaces are no longer available in the new version. Http://cxf.apache.org/docs/30-migration-guide.html)

But there is also an alternative (http://cxf.apache.org/docs/jax-rs.html)

 

E.g. We can get all the desired information from the message, such as the called method;

OperationResourceInfo resourceInfo = (OperationResourceInfo) ex.get("org.apache.cxf.jaxrs.model.OperationResourceInfo");ClassResourceInfo classResourceInfo = resourceInfo.getClassResourceInfo();Path path = classResourceInfo.getPath();Class<?> resourceClass = classResourceInfo.getResourceClass();Method annotatedMethod = resourceInfo.getAnnotatedMethod();Annotation[] declaredAnnotations = annotatedMethod.getDeclaredAnnotations();Description annotation = annotatedMethod.getAnnotation(Description.class);

 

The description above is the annotation defined at the beginning of the article, so that a small problem can be solved.

 

Cxf-interceptor get call Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.