java—如何使用SpringRESTTemplate处理json类型请求和xml类型请求

bakd9h0s  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(535)

有时我会以json(来自rest客户机)格式和xml(来自使用jsp视图的表单)格式收到请求。当我编写如下所示的控制器类时,它将不允许请求xml(从表单使用jsp视图)。
这是我的问题。控制器类应允许这两种类型的请求。

@RequestMapping(value = "/home", method = RequestMethod.POST)
    public String getCustomer(@RequestBody HomeRequest homeRequestequest,
            HttpServletRequest request) {

        String response = homeService.getCustomerResponse(homeRequestequest, request);
         return response;
        }

请帮助解决这个问题。我使用的是3.2.4.0版本。

3z6pesqy

3z6pesqy1#

正如jbarrueta在评论中所说,您需要在controller中声明两个方法,例如。 getCustomerJson 以及 getCustomerXML .
第一种方法的Map是:

@RequestMapping(value = "/home", method = RequestMethod.POST, consumes = "application/json")

第二种方法的Map是:

@RequestMapping(value = "/home", method = RequestMethod.POST, consumes = "application/xml")

相关问题