Spring-JSON Used in Spring MVC

I stumbled upon the Spring-JSON library while looking to add Ajax support to my spring mvc webapp 2.5.

My question is, has anyone here used this library and what are your impressions?

Is there a better alternative to this?

+2


a source to share


1 answer


Perhaps this post - http://www.jroller.com/kaiulrich/entry/a_mappingjacksonjsonview_springframework_and_spring can give you a good idea of ​​which Json View you should choose.

UPDATE

Suppose here is my MultiActionController

import static org.springframework.validation.ValidationUtils.*;

@Component
public class PersonController extends MultiActionController {

    /**
      * Notice my own JsonView implementation
      */ 
    private JsonView jsonView = new JsonView();

    public PersonController() {
        setValidators(new Validator[] {new Validator() {
            public boolean supports(Class clazz) {
                return clazz.isAssignableFrom(Person.class);
            }

            public void validate(Object command, Errors errors) {
                rejectIfEmpty(errors, "age", "", "Age is required");
                rejectIfEmptyOrWhitespace(errors, "name", "", "Name is required");
            }

        }});
    }

    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, Person person) throws Exception {
        // do something (save our Person object, for instance)

        return new ModelAndView(jsonView);
    }

    /**
      * If something goes wrong (validation, data-binding)
      * This method takes care of handling "what goes wrong"
      */
    public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
        BindException bindException = (BindException) bindingException.getRootCause();

        /**
          * Notice i am using jsonView to handle the response
          */
        return new ModelAndView(jsonView).addAllObjects(bindException.getModel());
    }

}    

      



The JsonView implementation is shown as follows

public class JsonView implements View {

    @Autowired
    private MessageSource messageSource;

    public String getContentType() {
        return "application/json";
    }

    public void render(Map model, HttpServletRequest request, HttpServletResponse response) {
        PrintWriter writer = response.getWriter();

        response.setContentType("text/plain; charset=UTF-8");

        JSONObject jsonObject = new JSONObject();
        for (Object object : bindingResult.getAllErrors()) {
            if(object instanceof FieldError) {
                FieldError fieldError = (FieldError) object;

                jsonObject.put(fieldError.getField(), messageSource.getMessage(fieldError, null));
            }
        }

        writer.print(jsonObject.toString());
        writer.close();
    }

}

      

I hope this can be helpful for you.

Here you can see a good tutorial on using Json DOJO (I do not use DOJO)

+2


a source







All Articles