Spring REST 消费导致 HTTP 状态 406 - 不可接受

2024-03-09

当我尝试使用 REST API 时出现此错误:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 406 Not Acceptable

这是执行的客户端代码:

public static void main(String[] args) {
   Car c = getCarById(4);
   System.out.println(c);
}

public static  @ResponseBody Car getCarById(int id){
    return new RestTemplate().getForObject("http://localhost:8080/rest/cars/{id}", Car.class, id);
}

这是映射请求的控制器的代码:

@RequestMapping(value="/cars/{id}", method=RequestMethod.GET, headers = {"Accept=text/html,application/xhtml+xml,application/xml"}, produces="application/xml")
public @ResponseBody Car getCarById(@PathVariable("id") int id){
    return carService.getCarById(id);
}

尽管映射器应该负责映射到正确的类型,但为什么会发生此错误(406-不可接受)?


您正在发送一个Accept=标题而不是Accept: header.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring REST 消费导致 HTTP 状态 406 - 不可接受 的相关文章

随机推荐