JSON - Spring MVC : How to post json data to spring MVC controller

前端 未结 2 1274
[愿得一人]
[愿得一人] 2021-01-05 21:29

I have a problem posting JSON data from jsp to controller. Everytime I try I get an ajax error Bad Request. Im so new to JSON and I re

相关标签:
2条回答
  • 2021-01-05 22:09

    If you want your JSON to be deserialized into some class, than you have to define method like this (and don't forget to add jsonConverter, as in previous answer):

    .... method(@RequestBody MyClass data){ ... }
    

    But, if you want your method to accept JSON as String than do this:

    .... method(@RequestBody String json){ ... }
    

    So, basically, if you post JSON, it means that JSON is not a parameter, it is body of the request. And eventually you have to use @RequestBody annotation, instead of @RequestParam.

    You can find beautifull video tutorial of Spring Mvc and JSON here: sites.google.com/site/upida4j/example

    0 讨论(0)
  • 2021-01-05 22:14

    it seems you dont have a Json Converter configured properly

    like this one

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <list>
        <ref bean="jacksonMessageConverter"/>
      </list>
    </property>
    </bean>
    
    0 讨论(0)
提交回复
热议问题