spring mvc rest response json and xml

后端 未结 7 1916
無奈伤痛
無奈伤痛 2020-12-28 19:24

I have the requirement to return the result from the database either as a string in xml-structure or as json-structure. I\'ve got a solution, but I don\'t know, if this one

7条回答
  •  孤独总比滥情好
    2020-12-28 19:58

    Here I wrote method which take XML as request parameter from your request mapping URL

    Here I am posting XML to my URL "baseurl/user/createuser/"

        public class UserController {
        @RequestMapping(value = "createuser/" ,
        method=RequestMethod.POST,  consumes= "application/xml")
        @ResponseBody
        ResponseEntity createUser(@RequestBody String requestBody ) {
    
            String r = "10"; // Put whatever response u want to return to requester
    
        return new ResponseEntity(
                  "Handled application/xml request. Request body was: " 
                  + r, 
                  new HttpHeaders(), 
                  HttpStatus.OK);       
    
    
        }
    }
    

    I tested it using chrome poster where you can send any xml in content body like:

    " 3saurabhshripasstest@test.com"
    

    This XML will capture by my createUser method and stored in String requestBody which i can use further

提交回复
热议问题