首页 > 解决方案 > Renaming an XML/SOAP tag using Apache CXF

问题描述

I've got a SOAP web-service server using Apache CXF as implementation. Due to some external technical constraint I'd like to be able to rename some XML tags naming an operation parameter (which are deprecated) in the inbound SOAP request. I was reading about using Interceptors for this, but the documentation on how to setup/configure them is not very clear.

My code to publish an endpoint is the following:

Endpoint endpoint = Endpoint.create(
    "http://schemas.xmlsoap.org/soap/", new MyServer());
endpoint.publish("ws/endpoint");

Ideally I'd like to add a filter only to a given endpoint (I have several of them).

标签: soapcxf

解决方案


Apache's documentations about interceptors are quite clear (IMO), anyway, there is a helloworld project (based on spring boot, cxf and maven) in my github profile which you can take a look for setting up interceptors (in fact it's a baisc autentication interceptor).

For setting up an interceptor (e.g InInterceptor), your class should extend AbstractPhaseInterceptor<Message> and override handleMessage(Message message) method, then in the constructor you should declare the phase in which the interceptor is going to be applied. Finally you have to instantiate it and apply in on an Endpoint.

As you said:

rename some XML tags naming an operation parameter (which are deprecated) in the inbound SOAP request

I think the name of the operation parameter (in WSDL file) is something different from the argument of your web method. Suppose that there is method in your endpoint named addPerson:

@WebMethod
String addPerson(Person person) {
    /*method logic*/
}

and Person class:

class Person {
    private String firstName;
    private String lastName;
    private Date birthDate;
    //getters and setters
}   

in order to map lastName property to a different name, you have to annotate it with

@XmlElement(name = "sureName")
private String lastName;

after applying this anotation, sureName (in wsdl file) is going to be mapped to lastName.

In addition, there is @WebParam annotation which can be used for changing the name of web method arguments:

@WebMethod
String sayHello( @WebParam(name = "sureName") String lastName);

Hope it helps.


推荐阅读