CXF Convert Number To String

CXF Convert Number To String explains about resolving the issue on serialization when the value of a string is integer when using jettison library

Jettison is an open source java library for processing JSON format. It is the default library bundled with CXF framework, the problem with jettison is that if value of a string is integer type, it automatically convert that string to integer type. By removing the quotes between that number

if the string values is "12345"
{"Student":{"name":"12345"}}
it automatically convert to below value
you can see that quotes are removed even the type of that object is string
{"Student":{"name":12345}}

In order to avoid this issue, I am going to use Jackson library by replacing Jettison

I am going to reuse Apache CXF With Jackson

Modify ChangeStudentDetailsImpl

Here I am setting an integer value into student name, just for showing the issue

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")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) {
   
student.setName("12345");
   
return student;
 
}

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

you can also see CXF Restful Client in order to run this restful service

Output when invoking the POST method using jettison
{"Student":{"name":12345}}
Output when invoking the POST method using Jackson
{"Student":{"name":"12345"}}

From the above output you can see that in jettison quotes are removed even the type of that property is string, it automatically converted into integer type






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