“找不到可接受的表示形式”使用 spring-boot-starter-web
2022-08-31 10:08:48
我正在尝试使用来创建一个 rest 服务,提供 Java 对象的 JSON 表示形式。据我所知,这个boot-starter-web jar应该自动处理通过Jackson转换为JSON的过程,但我反而得到了这个错误。spring-boot-starter-web
{
"timestamp": 1423693929568,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation"
}
我的控制器是这个...
@RestController
@RequestMapping(value = "/media")
public class MediaController {
@RequestMapping(value = "/test", method = RequestMethod.POST)
public @ResponseBody UploadResult test(@RequestParam(value="data") final String data) {
String value = "hello, test with data [" + data + "]";
return new UploadResult(value);
}
@RequestMapping(value = "/test2", method = RequestMethod.POST)
public int test2() {
return 42;
}
@RequestMapping(value = "/test3", method = RequestMethod.POST)
public String test3(@RequestParam(value="data") final String data) {
String value = "hello, test with data [" + data + "]";
UploadResult upload = new UploadResult(value);
return upload.value;
}
public static class UploadResult {
private String value;
public UploadResult(final String value)
{
this.value = value;
}
}
}
我的...pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.1.RELEASE</version>
<scope>provided</scope>
</dependency>
mvn dependency:tree
表明 spring-boot-starter-web 确实依赖于 jackson2.4 数据绑定,因此应该在类路径上...
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.2.1.RELEASE:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.8:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.8:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.14:runtime
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.4:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.4.4:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] | +- org.springframework:spring-web:jar:4.1.4.RELEASE:compile
[INFO] | | +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile
[INFO] | | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | | +- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:4.1.4.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.1.4.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
...但是调用该服务会给出上面提到的错误。 并且工作正常,证明它必须只是尝试转换为JSON失败?我是否遗漏了一些配置问题或注释?从我能找到的所有示例中,不再需要为基本的 Jackson JSON 转换对类进行注释。test
test2
test3
任何帮助都非常感谢。