JSON parameter in spring MVC controller

后端 未结 5 530
感动是毒
感动是毒 2020-11-27 04:51

I have

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
SessionInfo register(UserProfile profileJson){
  ...
}

I pass profileJson

相关标签:
5条回答
  • 2020-11-27 05:25

    You can create your own Converter and let Spring use it automatically where appropriate:

    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.core.convert.converter.Converter;
    import org.springframework.stereotype.Component;
    
    @Component
    class JsonToUserProfileConverter implements Converter<String, UserProfile> {
    
        private final ObjectMapper jsonMapper = new ObjectMapper();
    
        public UserProfile convert(String source) {
            return jsonMapper.readValue(source, UserProfile.class);
        }
    }
    

    As you can see in the following controller method nothing special is needed:

    @GetMapping
    @ResponseBody
    public SessionInfo register(@RequestParam UserProfile userProfile)  {
      ...
    }
    

    Spring picks up the converter automatically if you're using component scanning and annotate the converter class with @Component.

    Learn more about Spring Converter and type conversions in Spring MVC.

    0 讨论(0)
  • 2020-11-27 05:29

    Just add @RequestBody annotation before this param

    0 讨论(0)
  • 2020-11-27 05:38

    This does solve my immediate issue, but I'm still curious as to how you might pass in multiple JSON objects via an AJAX call.

    The best way to do this is to have a wrapper object that contains the two (or multiple) objects you want to pass. You then construct your JSON object as an array of the two objects i.e.

    [
      {
        "name" : "object1",
        "prop1" : "foo",
        "prop2" : "bar"
      },
      {
        "name" : "object2",
        "prop1" : "hello",
        "prop2" : "world"
      }
    ]
    

    Then in your controller method you recieve the request body as a single object and extract the two contained objects. i.e:

    @RequestMapping(value="/handlePost", method = RequestMethod.POST, consumes = {      "application/json" })
    public void doPost(@RequestBody WrapperObject wrapperObj) { 
         Object obj1 = wrapperObj.getObj1;
         Object obj2 = wrapperObj.getObj2;
    
         //Do what you want with the objects...
    
    
    }
    

    The wrapper object would look something like...

    public class WrapperObject {    
    private Object obj1;
    private Object obj2;
    
    public Object getObj1() {
        return obj1;
    }
    public void setObj1(Object obj1) {
        this.obj1 = obj1;
    }
    public Object getObj2() {
        return obj2;
    }
    public void setObj2(Object obj2) {
        this.obj2 = obj2;
    }   
    
    }
    
    0 讨论(0)
  • 2020-11-27 05:39

    The solution to this is so easy and simple it will practically make you laugh, but before I even get to it, let me first emphasize that no self-respecting Java developer would ever, and I mean EVER work with JSON without utilizing the Jackson high-performance JSON library.

    Jackson is not only a work horse and a defacto JSON library for Java developers, but it also provides a whole suite of API calls that makes JSON integration with Java a piece of cake (you can download Jackson at http://jackson.codehaus.org/).

    Now for the answer. Assuming that you have a UserProfile pojo that looks something like this:

    public class UserProfile {
    
    private String email;
    // etc...
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    // more getters and setters...
    }
    

    ...then your Spring MVC method to convert a GET parameter name "profileJson" with JSON value of {"email": "mymail@gmail.com"} would look like this in your controller:

    import org.codehaus.jackson.JsonParseException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper; // this is your lifesaver right here
    
    //.. your controller class, blah blah blah
    
    @RequestMapping(value="/register", method = RequestMethod.GET) 
    public SessionInfo register(@RequestParam("profileJson") String profileJson) 
    throws JsonMappingException, JsonParseException, IOException {
    
        // now simply convert your JSON string into your UserProfile POJO 
        // using Jackson's ObjectMapper.readValue() method, whose first 
        // parameter your JSON parameter as String, and the second 
        // parameter is the POJO class.
    
        UserProfile profile = 
                new ObjectMapper().readValue(profileJson, UserProfile.class);
    
            System.out.println(profile.getEmail());
    
            // rest of your code goes here.
    }
    

    Bam! You're done. I would encourage you to look through the bulk of Jackson API because, as I said, it is a lifesaver. For example, are you returning JSON from your controller at all? If so, all you need to do is include JSON in your lib, and return your POJO and Jackson will AUTOMATICALLY convert it into JSON. You can't get much easier than that. Cheers! :-)

    0 讨论(0)
  • 2020-11-27 05:45

    This could be done with a custom editor, that converts the JSON into a UserProfile object:

    public class UserProfileEditor extends PropertyEditorSupport  {
    
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            ObjectMapper mapper = new ObjectMapper();
    
            UserProfile value = null;
    
            try {
                value = new UserProfile();
                JsonNode root = mapper.readTree(text);
                value.setEmail(root.path("email").asText());
            } catch (IOException e) {
                // handle error
            }
    
            setValue(value);
        }
    }
    

    This is for registering the editor in the controller class:

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(UserProfile.class, new UserProfileEditor());
    }
    

    And this is how to use the editor, to unmarshall the JSONP parameter:

    @RequestMapping(value = "/jsonp", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    SessionInfo register(@RequestParam("profileJson") UserProfile profileJson){
      ...
    }
    
    0 讨论(0)
提交回复
热议问题