使用spring-boot-starter-web"无法找到可接受的表示"

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),在您的示例中将导致[].

  • well spring对HttpMediaTypeNotAcceptableException错误消息非常"有用"... (19认同)
  • 我遇到了同样的问题,有一个没有字段或getter的空类(到目前为止).我同意错误信息非常"有用",所以非常感谢. (2认同)
  • 我试图返回`List&lt;MyClass&gt;`,但说`produces = MediaType.APPLICATION_XML_VALUE` 所以Spring 不知道如何将列表表示为XML .. :D 显然...... (2认同)

Abh*_*pta 19

如果您使用Lombok,请确保它在您的响应模型类中具有@Data 或 @Getter @Setter等注释。


wir*_*d00 12

使用spring/jhipsterRESTful服务(通过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

  • 是的,我认为“无法找到可接受的代表”会以多种方式暴露其丑陋的一面。:( (3认同)
  • 我有一个 JSON API,但在一条路线中,我返回了 `produces = MediaType.TEXT_HTML_VALUE`(原始 HTML ..)。但是当抛出异常时(我想要一个 JSON 错误模型),Spring 可能不知道如何将它转换为字符串...... (2认同)

小智 8

我也面临着类似的问题。在我的情况下,请求路径接受邮件ID作为路径变量,因此uri看起来像/some/api/test@test.com

并且基于路径,Spring确定uri将获取扩展名为“ .com”的文件,并尝试使用其他媒体类型进行响应,然后使用预期的媒体类型。将路径变量设置为请求参数后,它对我有用。


cha*_*rlb 6

如果使用@FeignClient,添加例如

produces = "application/json"
Run Code Online (Sandbox Code Playgroud)

到@RequestMapping注解


Gau*_*iya 5

将以下依赖项添加到您的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)

  • 当缺少这个依赖项时,我们会得到同样的错误。因此,请不要否决这个答案,因为这在某些情况下是正确的。 (3认同)

小智 5

即使您执行了上述所有操作,就像我的情况一样,我仍然收到错误 406 - 从邮递员使用时不可接受。经过仔细研究,我注意到,在请求标头中,“accept”的默认标头是“ / ”。

我通过向标题添加预设并关闭默认标题解决了我的问题。请参阅下面的屏幕截图。

邮递员预设的屏幕截图

这解决了我的问题,无需任何其他设置,甚至无需添加弹簧配置。