gpt4 book ai didi

java - Spring REST 406 Http 错误

转载 作者:可可西里 更新时间:2023-11-01 16:36:14 27 4
gpt4 key购买 nike

编辑:已修复!无需回复

我必须为学校创建一个项目,但找不到解决这个不断出现的 HTTP 错误的方法...

我会尽量使代码尽可能短而不遗漏任何内容。

我正在使用带有 XML 配置的 Spring MVC:

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
etc..>

<context:component-scan base-package="ui.controller"/>
<mvc:annotation-driven/>
</beans>

Pom.xml:

  <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.3</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>

休息 Controller :

@RestController
@RequestMapping(value = "/rest")
public class ProductRESTController {

private final ProductService service;

public ProductRESTController(@Autowired ProductService service) {
this.service = service;
}

@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
public List<Product> getProducts() {
return service.getAllProducts();
}
}

我们必须使用 Postman 来检查我们的 REST Controller 的功能,所以我也会发布标题代码:

GET /SchoolProject/rest.htm HTTP/1.1
Host: localhost:8080
Accept: application/json
Cache-Control: no-cache
Postman-Token: 1543765c-b6c0-c82a-6c7d-6e4ce445fa16

我尝试了很多东西,多次更改代码,但没有任何效果。我不断收到 406 HTTP 错误:

"The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."

即使我做客户端和服务器端应用程序/Json...

请帮忙!

最佳答案

您应该在@RequestMappings 上使用“produces”属性:

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public List<Product> getProducts() {
return service.getAllProducts();
}
}

关于java - Spring REST 406 Http 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45488944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com