spring mvc rest response json and xml

后端 未结 7 1919
無奈伤痛
無奈伤痛 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:55

    You can use ContentNegotiatingViewResolver as below:

    
        
        
        
        
        
            
                
                
            
        
        
            
                
                
            
        
    
    
    
        
        
    
    
    
        
        
            
        
    
    
    
    
        
        
        
    
    

    In your controller:

    @RequestMapping(value = "/get/response.json", method = RequestMethod.GET)
    public JSONResponse getJsonResponse(){
        return responseService.getJsonResponse();
    }
    @RequestMapping(value = "/get/response.xml", method = RequestMethod.GET)
    public  XMLResponse getXmlResponse(){
        return responseService.getXmlResponse();
    }
    

提交回复
热议问题