1

I am using Spring MVC 3 and a MappingJacksonHttpMessageConverter to serialize my java objects to JSON when they're sent to my client. My problem is that Java long values are being rounded in the client because Javascript numbers can't handle the precision of long values. To get around this, I'm going to send these fields as strings instead of longs. Is there any way to automatically have Spring convert longs to strings without me having to cast every return value in my controllers?

Jared
  • 2,043
  • 5
  • 33
  • 63
  • 1
    Why do it in the controller, just append a single function in the middleware. – Anders Oct 23 '12 at 18:29
  • I suppose what I am asking is where/how to append such a function. – Jared Oct 23 '12 at 18:31
  • Below link would help you to solve your problem [customized bean converter i.e., you can add LongToString converter class and map it with MappingJacksonHttpMessageConverter][1] [1]: http://stackoverflow.com/questions/7854030/configurating-objectmapper-in-spring – santhoshkumar Oct 17 '13 at 10:50

1 Answers1

0

You could copy object with adding new variable of type String using import org.apache.commons.beanutils.*;

public class Object {

String a;
Long b;

public String getA() {
    return a;
}
public void setA(String a) {
    this.a = a;
}
public Long getB() {
    return b;
}
public void setB(Long b) {
    this.b = b;
}}

public class Object2 extends Object{

String f;

public String getF() {
    return b.toString();
}}

    public static void main( String[] args ) throws IllegalAccessException, InvocationTargetException
{       
    Object m = new Object();
    m.setA("aa");
    m.setB((long) 22222);
    Object2 m2 = new Object2();

    BeanUtils.copyProperties(m2, m);

    //now you can convert m2 to JSONobject

}
Marcin Wasiluk
  • 4,675
  • 3
  • 37
  • 45