0

in Spring MVC, Rest Controller method converting List with single object as JSON array as:

{
["TestValue"]
}

But my requirement is that, it should be converted as

{"TestValue"}

so, whenever list holds single value, JSON serializer should convert list as JSON Object not as JSON Array.

any suggestions on configuring Jackson in Spring4 MVC?

Thanks -Trim

Trim Bandaru
  • 83
  • 2
  • 8

2 Answers2

0

If you wanna use your own protocol, you may just return String from method of Spring Controller. I wrote for you a little example. I used "jackson-jaxrs-json-provider) for mapping. You may create you own function for converting your beans. It's very easy.

For example you have a class:

public class MyBean {
    private String name;
    private String value;
    private List<MyBean> beanList = new ArrayList<>();

    public MyBean(String name, String value, MyBean previous) {
        this.name = name;
        this.value = value;
        if (previous != null)
            this.beanList.add(previous);
    }

    //getters & setters
}

The you create simple function for convertation:

public static String convertToMyJson(Object value) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        String resultList = mapper.writeValueAsString(value);
        resultList = "{" + resultList.replaceAll("(^\\[)|(\\]$)", "") + "}";
        System.out.println(resultList);
    }
    catch(Exception e) {
        e.printStackTrace(System.err);
    }
    return "";
}

And in the end, you just return simple string in Spring MVC Controller:

@RequestMapping(path = "get-bean", method = RequestMethod.GET)
public String getBean() {
    MyBean bean1 = new MyBean("name1", "11", null);
    MyBean bean2 = new MyBean("name2", "22", bean1);
    List<MyBean> beanList = new ArrayList<>();
    beanList.add(bean1);
    beanList.add(bean2);

    return convertToMyJson(beanList);
}

And you'll get something like that:

{  
   {  
      "name":"name1",
      "value":"11",
      "beanList":[
      ]
   },
   {  
      "name":"name2",
      "value":"22",
      "beanList":[  
         {  
            "name":"name1",
            "value":"11",
            "beanList":[  
            ]
         }
      ]
   }
}
Ivan
  • 992
  • 1
  • 10
  • 22
0

If you know that the list contains a single object, then you could serialize the first element of the list instead of the entire list.

But if your list could contain any number of objects (zero, one, many), then treating the case of a one-element list differently could be a bad idea. If you do that, then the client / service that receives the JSON now has to cope with two different "shapes" of JSON. That makes it more complicated ... for minimal tangible benefit1.


1 - Looking "neat" is not a tangible benefit. Saving 2 characters is unlikely to make a significant difference.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • agree with your comment @Stephen. I am trying to implement OData server library that generates response processable by SAP OData client.as OData client expecting me to return response as I specified in post.I have to manipulate response unwillingly:( – Trim Bandaru Jun 03 '16 at 10:24