HTTP状态406 - 在Spring MVC Rest Service

时间:2018-02-12 06:46:38

标签: java spring rest

这是我的代码:

@RequestMapping(value = "/report/download", method = RequestMethod.GET,produces="application/vnd.ms-excel")
    public Response getReportFile(@QueryParam("reportid") Long reportId)
    {
        System.out.println("Param"+reportId);
        Long n=(long) 10;
        String json=reportService.getReportFile(n);
        File file = new File("D:\\Agent Information.xls");  
        ResponseBuilder response = Response.ok((Object) file);  
        response.header("Content-Disposition","attachment; filename=Sample.xls");  
        return response.build();  
    }

我在java控制台中遇到以下错误:处理程序执行导致异常:找不到可接受的表示

1 个答案:

答案 0 :(得分:0)

您的网络服务表示客户请求中的Accept HTTP header未提供其返回的回复类型。

因此,在发出HTTP请求时,您必须添加“接受标头”。如果是JSON请求,那么您将添加'Accept : application/json'。同样对于您当前的示例,它将是

'Accept: text/plain'
'Accept-Charset: utf-8'

在此处查找所有accept headers。并按照以下步骤解决

1)找出网络服务返回的response (content type)

2)在请求接受标题中提供此内容类型。