Change CXF Character Encoding

Change CXF Character Encoding explains about How to change character encoding of CXF based messages

Usually CXF not change the encoding of messages, but you can deliberately change one encoding to another by specifying a particular charset that we needed.

Here I am showing an CXF Character Encoding Example where you can see How to change one encoding to another, covering both JAX-WS & JAX-RS services

Change Character Encoding In CXF Restful Services

I am going to re-use CXF Restful Tutorial

We are making changes ChangeStudentDetailsImpl class, just adding

@Produces("application/json; charset=UTF-8")
package com.student;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Consumes("application/json")
@Produces("application/json; charset=UTF-8")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) {
   
student.setName("HELLO " + student.getName());
   
return student;
 
}

 
@GET
  @Path
("/getName")
 
public Student getName() {
   
Student student = new Student();
    student.setName
("Rockey");
   
return student;
 
}
}

Run CXF REST Client

package com.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

//CXF REST/JAX-RS Encoding Example

public class PostStudentClient {

 
public static void main(String[] args) {
   
try {
     
URL url = new URL("http://localhost:8082/CXFRestfulTutorial/rest/changeName");
      HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
      conn.setDoOutput
(true);
      conn.setRequestMethod
("POST");
      conn.setRequestProperty
("Content-Type", "application/json;charset=ISO-8859-1");

      String input =
"{\"Student\":{\"name\":\"Tom\"}}";

      OutputStream os = conn.getOutputStream
();
     
      os.write
(input.getBytes());
      os.flush
();

      Scanner scanner;
      String response;
     
if (conn.getResponseCode() != 200) {
       
scanner = new Scanner(conn.getErrorStream());
        response =
"Error From Server \n\n";
     
} else {
       
System.out.println(conn.getContentType());
        scanner =
new Scanner(conn.getInputStream());
        response =
"Response From Server \n\n";
     
}
     
scanner.useDelimiter("\\Z");
      System.out.println
(response + scanner.next());
      scanner.close
();
      conn.disconnect
();
   
} catch (MalformedURLException e) {
     
e.printStackTrace();
   
} catch (IOException e) {
     
e.printStackTrace();
   
}
  }
}
Output

From the output screenshot you can see that even we are setting charset as ISO-8859-1 in client side by using the below code

conn.setRequestProperty("Content-Type", "application/json;charset=ISO-8859-1");

Response payload producing UTF-8 charset

Change CXF Character Encoding In Restful Services

1 2 Next





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