cro*_*umb 57 java spring json spring-mvc maven
我试图用来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"
}
Run Code Online (Sandbox Code Playgroud)
我的控制器就是这个......
@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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我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>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
...但是调用test
服务会给出上面提到的错误.test2
并且test3
工作正常,证明它必须只是尝试转换为失败的JSON?我错过了一些配置问题或注释吗?从我可以找到的所有示例中,不再需要为基本的Jackson JSON转换注释类.
任何帮助非常感谢.
iku*_*men 75
您的UpdateResult没有公共getter,例如:
public static class UploadResult {
private String value;
public UploadResult(final String value)
{
this.value = value;
}
public String getValue() {
return this.value;
}
}
Run Code Online (Sandbox Code Playgroud)
我相信默认情况下自动发现功能已开启,并会尝试发现您的吸气剂.您可以使用它来禁用它@JsonAutoDetect(getterVisibility=Visibility.NONE)
,在您的示例中将导致[]
.
wir*_*d00 12
使用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) {
Run Code Online (Sandbox Code Playgroud)
我试图restful
通过Postman
标题调用端点,Accept: text/plain
但我需要使用Accept: application/json
小智 8
我也面临着类似的问题。在我的情况下,请求路径接受邮件ID作为路径变量,因此uri看起来像/some/api/test@test.com
并且基于路径,Spring确定uri将获取扩展名为“ .com”的文件,并尝试使用其他媒体类型进行响应,然后使用预期的媒体类型。将路径变量设置为请求参数后,它对我有用。
如果使用@FeignClient,添加例如
produces = "application/json"
Run Code Online (Sandbox Code Playgroud)
到@RequestMapping注解
将以下依赖项添加到您的pom.xml
:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
小智 5
即使您执行了上述所有操作,就像我的情况一样,我仍然收到错误 406 - 从邮递员使用时不可接受。经过仔细研究,我注意到,在请求标头中,“accept”的默认标头是“ / ”。
我通过向标题添加预设并关闭默认标题解决了我的问题。请参阅下面的屏幕截图。
这解决了我的问题,无需任何其他设置,甚至无需添加弹簧配置。
归档时间: |
|
查看次数: |
113455 次 |
最近记录: |