我正在尝试使用spring-boot-starter-web
创建一个提供Java对象的JSON表示的休息服务。根据我的理解,这个boot-starter-web jar应该自动处理通过Jackson转换为JSON,但我得到了这个错误。
{ "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
...但是调用test
服务会给出上面提到的错误。 test2
和test3
工作正常,证明它必须只是尝试转换为失败的JSON?我错过了一些配置问题或注释吗?从我可以找到的所有示例中,不再需要为基本的Jackson JSON转换注释类。
任何帮助非常感谢。
您的UpdateResult没有公共getter,例如:
public static class UploadResult {
private String value;
public UploadResult(final String value)
{
this.value = value;
}
public String getValue() {
return this.value;
}
}
我相信默认情况下自动发现功能已开启,并会尝试发现您的吸气剂。您可以使用@JsonAutoDetect(getterVisibility=Visibility.NONE)
禁用它,在您的示例中将导致[]
。
在我的情况下,我碰巧使用了lombok,显然与get和set存在冲突
在要反序列化的pojo类上添加@Data注释。问题解决了我:)
例如,
import lombok.Data;
@Data
public class Response {
private Integer status;
private String type;
}
我有同样的例外。问题是,我使用了注释
@RepositoryRestController
代替
@RestController
我使用spring/jhipster
RESTful服务(通过Postman
)有类似的错误
端点是这样的:
@RequestMapping(value = "/index-entries/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<IndexEntry> getIndexEntry(@PathVariable Long id) {
我试图通过restful
用标题Postman
调用Accept: text/plain
端点,但我需要使用Accept: application/json
我也面临着类似的问题。在我的情况下,请求路径接受邮件ID作为路径变量,所以uri看起来像/some/api/[email protected]
并且基于路径,Spring确定uri是获取一些带有“.com”扩展名的文件,并试图使用不同的媒体类型进行响应。在将路径变量变为请求参数之后,它对我有用。
我的返回对象在类上没有@XmlRootElement注释。添加注释解决了我的问题。
@XmlRootElement(name = "ReturnObjectClass")
public class ReturnObjectClass {
@XmlElement(name = "Status", required = true)
protected StatusType status;
//...
}
如果使用@FeignClient,请添加例如
produces = "application/json"
到@RequestMapping注释
添加以下依赖项:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.4.6</version>
</dependency>
我必须在我的POM中显式调出我的json库的依赖项。
一旦我添加了以下依赖项,这一切都有效。
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
在Spring 5中接受的答案是不正确的。尝试将您的Web服务的URL更改为.json!这是正确的解决方案。这里有很多细节http://stick2code.blogspot.com/2014/03/solved-orgspringframeworkwebhttpmediaty.html
我有相同的例外,但原因不同,我找不到任何信息,所以我会在这里发布。这只是一个容易忽视的错误。
坏:
@RestController(value = "/api/connection")
好:
@RestController
@RequestMapping(value = "/api/connection")