编辑:固定!无需回复
我必须为学校创建一个项目,我无法找到这个不断出现的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>
休息控制器:
@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控制器的功能,所以我也会发布标题代码:
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."
即使我做客户端&amp;服务器端Application / Json ......
请帮忙!
答案 0 :(得分:1)
你应该使用&#39;产生&#39; @RequestMappings上的属性:
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public List<Product> getProducts() {
return service.getAllProducts();
}
}
答案 1 :(得分:0)
@RequestMapping(value="/getProducts",method = RequestMethod.GET,consumes=MediaType.APPLICATION_JSON_UTF8_VALUE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Product> getProducts() {
return service.getAllProducts();
}
GET / getProducts
答案 2 :(得分:0)
这是你的xml中的内容吗?看看。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">