JSON post to Spring Controller

前端 未结 5 1388
清酒与你
清酒与你 2020-12-24 08:50

Hi I am starting with Web Services in Spring, so I am trying to develop small application in Spring + JSON + Hibernate. I have some problem with HTTP-POST. I created a metho

相关标签:
5条回答
  • 2020-12-24 09:06

    Try to using application/* instead. And use JSON.maybeJson() to check the data structure in the controller.

    0 讨论(0)
  • 2020-12-24 09:10

    see here

    The consumable media types of the mapped request, narrowing the primary mapping.

    the producer is used to narrow the primary mapping, you send request should specify the exact header to match it.

    0 讨论(0)
  • 2020-12-24 09:12

    You need to include the getters and setters for all the fields that have been defined in the model Test class --

    public class Test implements Serializable {
    
        private static final long serialVersionUID = -1764970284520387975L;
    
        public String name;
    
        public Test() {
    
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 09:20

    Convert your JSON object to JSON String using

    JSON.stringify({"name":"testName"})

    or manually. @RequestBody expecting json string instead of json object.

    Note:stringify function having issue with some IE version, firefox it will work

    verify the syntax of your ajax request for POST request. processData:false property is required in ajax request

    $.ajax({ 
        url:urlName,
        type:"POST", 
        contentType: "application/json; charset=utf-8",
        data: jsonString, //Stringified Json Object
        async: false,    //Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation
        cache: false,    //This will force requested pages not to be cached by the browser  
         processData:false, //To avoid making query String instead of JSON
         success: function(resposeJsonObject){
            // Success Action
        }
    });
    

    Controller

    @RequestMapping(value = urlPattern , method = RequestMethod.POST)
    
    public @ResponseBody Test addNewWorker(@RequestBody Test jsonString) {
    
        //do business logic
        return test;
    }
    

    @RequestBody -Covert Json object to java

    @ResponseBody - convert Java object to json

    0 讨论(0)
  • 2020-12-24 09:26

    Do the following thing if you want to use json as a http request and response. So we need to make changes in [context].xml

    <!-- Configure to plugin JSON as request and response in method handler -->
    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter"/>
            </beans:list>
        </beans:property>
    </beans:bean>
    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>   
    

    MappingJackson2HttpMessageConverter to the RequestMappingHandlerAdapter messageConverters so that Jackson API kicks in and converts JSON to Java Beans and vice versa. By having this configuration, we will be using JSON in request body and we will receive JSON data in the response.

    I am also providing small code snippet for controller part:

        @RequestMapping(value = EmpRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)
    
        public @ResponseBody Employee getDummyEmployee() {
        logger.info("Start getDummyEmployee");
        Employee emp = new Employee();
        emp.setId(9999);
        emp.setName("Dummy");
        emp.setCreatedDate(new Date());
        empData.put(9999, emp);
        return emp;
    }
    

    So in above code emp object will directly convert into json as a response. same will happen for post also.

    0 讨论(0)
提交回复
热议问题