Change CXF Character Encoding

Change Character Encoding In CXF JAX-WS Services

I am going to re-use CXF Web Service Tutorial

We are making changes ChangeStudentDetailsImpl class, just adding

Message message = PhaseInterceptorChain.getCurrentMessage();
message.put(Message.ENCODING, "ASCII");
package com.student;

import javax.jws.WebService;

import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;

@WebService(endpointInterface = "com.student.ChangeStudentDetails")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
 
public Student changeName(Student student) {
   
student.setName("Hello " + student.getName());
    Message message = PhaseInterceptorChain.getCurrentMessage
();
    message.put
(Message.ENCODING, "ASCII");
   
return student;
 
}
}
Note

In order to run the client, you need to add cxf libraries to class path, you can see this on below article

Create CXF Client Example

Run CXF SOAP Client

package com.client;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.student.ChangeStudentDetails;
import com.student.Student;

//CXF JAX-WS Encoding Example

public final class StudentClient {

 
public static void main(String args[]) throws Exception {

   
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

    factory.setServiceClass
(ChangeStudentDetails.class);
    factory.setAddress
("http://localhost:8080/CXFTutorial/ChangeStudent?wsdl");
    factory.getInInterceptors
().add(new LoggingInInterceptor());
    factory.getOutInterceptors
().add(new LoggingOutInterceptor());
    ChangeStudentDetails client =
(ChangeStudentDetails) factory.create();
    Student student =
new Student();
    student.setName
("Rockey");
    Student changeName = client.changeName
(student);
    System.out.println
("Server said: " + changeName.getName());
    System.exit
(0);
 
}
}
Output

From the output screenshot you can see that Response payload producing ASCII charset

Change CXF Character Encoding In JAX-WS Services




Previous 1 2








Your email address will not be published. Required fields are marked *