Hello,
In this mini article, we will explain the 2 ways to generate JSON from a web application based on Spring MVC:
– with the “JSON view resolver” of Spring;
– without the “JSON view resolver” of Spring i.e. with the json-lib-2.3-jdk15.jar;
Reminder: Classic handler returning to a JSP page due to ‘JstlView’
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
16 | < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > |
17 | < property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" ></ property > |
18 | < property name = "prefix" value = "/WEB-INF/jsp/" ></ property > |
19 | < property name = "suffix" value = ".jsp" ></ property > |
20 | < property name = "order" >< value >2</ value ></ property > |
… and the view resolver JstlView is used in the spring controller like:
02 | * Controler SPRING MVC: Class to handle the web requests. |
07 | public class MyController extends MultiActionController { |
10 | * <p> Handler of Spring controller using the Spring Internal Resource view resolver</p> |
12 | public ModelAndView handleWithSpringSpringInternalResourceResolver(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, InterruptedException { |
15 | request.setAttribute( "ControllerData" , data); |
18 | return new ModelAndView( "/myJSPPage" ); |
JSON with the “JSON view resolver” of Spring
Often the web applications based on Spring MVC, use the “JSON view resolver” of Spring:
03 | < bean id = "jsonResolver" class = "org.springframework.web.servlet.view.BeanNameViewResolver" > |
04 | < property name = "order" >< value >1</ value ></ property > |
07 | < bean name = "jsonView" class = "org.springframework.web.servlet.view.json.JsonView" > |
08 | < property name = "contentType" > |
09 | < value >text/html</ value > |
… and the view resolver JsonView is used in the spring controller like:
03 | * <p> Handler of Spring controller using the Spring JSON resolver</p> |
05 | public ModelAndView handleWithSpringJsonResolver(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, InterruptedException { |
07 | Map<String, Object> model = new HashMap<String, Object>(); |
08 | model.put( "success" , "true" ); |
09 | model.put( "data" , bean); |
10 | return new ModelAndView( "jsonView" , model); |
JSON without the “JSON view resolver” of Spring
It is possible to use an external library like json-lib-2.3-jdk15.jar to generate “manually” the JSON instead of the Spring json resolver. So, it is not necessary to configure a ViewResolver in spring web context.
First, we wil create a class ListOfDocuments containing a number of Document:
01 | public class ListOfDocuments{ |
04 | public JSONObject getJSON(){ |
05 | JSONObject jsonObj = new JSONObject(); |
06 | JSONArray jsonArray = new JSONArray(); |
07 | for (Document doc : this .documents){ |
08 | jsonArray.add(doc.getJSON()); |
10 | jsonObj.put( "listId" , this .listId); |
11 | jsonObj.put( "items" , jsonArray); |
04 | public JSONObject getJSON(){ |
05 | JSONObject jsonObj = new JSONObject(); |
06 | jsonObj.put( "documentId" , this .documentId); |
07 | jsonObj.put( "reference" , this .reference); |
… then, the spring controller could generate the JSON by directly calls the appropriate methods:
03 | * <p> Handler of Spring controller using the external JSON generator json-lib-2.3-jdk15.jar</p> |
05 | public ModelAndView handleWithExternalJSONGenerator(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, InterruptedException { |
06 | ListOfDocuments bean = null ; |
09 | reponse.setContentType( "application/json" ); |
10 | obj.write(reponse.getWriter()); |
More, it is possible to write a JSON Configuration to specify the behaviour during the jsonization:
01 | private static JsonConfig jsonConfigValidation; |
03 | jsonConfigValidation = getDefaultJsonConfig(); |
04 | jsonConfigValidation.setExcludes( new String[]{ "errorPresent" }); |
08 | private JsonConfig getDefaultJsonConfig(){ |
09 | JsonConfig config = new JsonConfig(); |
10 | config.registerJsonValueProcessor(Calendar. class , new JsonValueProcessor(){ |
12 | public Object processArrayValue(Object value, JsonConfig config){ |
13 | return process(value, config); |
17 | public Object processObjectValue(String key, Object value, JsonConfig config){ |
18 | return process(value, config); |
21 | private Object process(Object value, JsonConfig config){ |
22 | String txtValue = "12/12/2012" ; |
This JsonConfig could be used during the creation of JsonObjectlike:
1 | JSONObject jsonObj = new JSONObject(); |
2 | jsonObj.put( "errors" , JSONArray.fromObject(errors, jsonConfigValidation)); |
That’s all !!!!
Huseyin OZVEREN
Related
Koennte miг mal irgendwer sagen, wo ich andere solche Aufsaetze zu diesem Geɡenstand aufgabeln kann?