Spring MVC: Doesn't deserialize JSON request body

前端 未结 6 1428
忘掉有多难
忘掉有多难 2020-12-02 09:28

I\'m working on a Spring MVC project and one of the tasks I need to do requires me to have a string of JSON data sent through by the user in a POST request. I know that Spri

6条回答
  •  星月不相逢
    2020-12-02 10:01

    We had a situation where we wanted some controller methods to map the POST body to beans, and other methods where we just wanted the raw String. To accomplish this using the @RequestBody annotation, you need to configure multiple message converters, something like...

    
      
    
    
    
        
            
                
                
                
            
        
    
    
    
        
    
    
    
        
        
    
    
    
        
    
    

    Then, requests to the various methods must specify the "content-type" header with an appropriate value. For those methods where the request body is mapped to a JAXB bean, specify "application/xml". And for those where the request body is a String, use "text/plain".

提交回复
热议问题