Java 我的Spring引导服务器返回406 Http错误代码

Java 我的Spring引导服务器返回406 Http错误代码,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个Spring引导服务器应用程序,它接收来自外部服务的POST请求,具有以下特征: 标题 形式值 为了能够处理来自外部服务的POST请求,我实现了以下控制器,它所做的是创建和存储发票,但我的应用程序返回HTTP错误406: @RequestMapping(value = "/bills", method = RequestMethod.POST, headers = "Accept=application/x-www-form-urlencoded") @ResponseBody @Tra

我有一个Spring引导服务器应用程序,它接收来自外部服务的POST请求,具有以下特征:

标题

形式值

为了能够处理来自外部服务的POST请求,我实现了以下控制器,它所做的是创建和存储发票,但我的应用程序返回HTTP错误406:

@RequestMapping(value = "/bills", method = RequestMethod.POST, headers = "Accept=application/x-www-form-urlencoded")
@ResponseBody
@Transactional
public void createBill(UriComponentsBuilder uriComponentsBuilder,
        final HttpServletRequest request,
        final HttpServletResponse response) throws IOException {
}
我知道错误是指客户端。在这种情况下,外部服务不理解服务器响应的语言,但正如您在控制器的标题中看到的,我正在接受application/x-www-form-urlencoded。我不知道这是否是由于另一个问题,也是我的控制器,它是无效的

如何在我的spring boot应用程序中实现此控制器?

您应该使用consumes with来定义支持的内容类型。您可以使用以下方法进一步简化:

像现在一样在响应上设置Accept头是没有意义的,这是一个请求头。根据:

Accept-request-HTTP报头公布客户端能够理解的内容类型(以MIME类型表示)。使用内容协商,服务器然后选择其中一个方案,使用它,并使用内容类型响应头通知客户端其选择。浏览器根据执行请求的上下文为此标题设置足够的值:在获取CSS样式表时,为请求设置的值与获取图像、视频或脚本时设置的值不同


不,这意味着服务器无法生成客户端请求的内容。你把接受和内容类型搞混了,这是我想要的,这是我要发送的标题。
BillNumber: 41492032464
BillValue: 600000.0
Description: Description
@RequestMapping(value = "/bills", method = RequestMethod.POST, headers = "Accept=application/x-www-form-urlencoded")
@ResponseBody
@Transactional
public void createBill(UriComponentsBuilder uriComponentsBuilder,
        final HttpServletRequest request,
        final HttpServletResponse response) throws IOException {
}
@PostMapping(value = "/bills", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)