Spring Boot REST中的自定义媒体类型导致HttpMediaTypeNotSupportedException

时间:2017-03-23 20:07:07

标签: spring spring-boot http-post spring-restcontroller media-type

我使用的是Spring Boot 1.5.2,我有一个简单的REST控制器,如下所示,

@RestController
@RequestMapping
public class UserJWTController {

    @Inject
    private TokenProvider tokenProvider;

    @Inject
    private AuthenticationManager authenticationManager;

    @PostMapping(value = "/authenticate", produces = Version.V1_JSON_VALUE, consumes = Version.V1_JSON_VALUE)
    public ResponseEntity<?> authenticate(final @RequestBody LoginVM loginVM, HttpServletResponse response) {
    }
}

常数,

public final class Constants {
    // API versioning
    public static final String API_VERSION_MEDIA_TYPE = "application/vnd.test.onboarding";
    public static final String JSON_MIME_TYPE = "json";

    private Constants() {
    }
}

版,

public class Version {
    // All versions defined here
    public static final String V1_JSON_VALUE = Constants.API_VERSION_MEDIA_TYPE + ".v1+" + Constants.JSON_MIME_TYPE;
    public static final MediaType V1_JSON = MediaType.valueOf(V1_JSON_VALUE);
    public static final String V2_JSON_VALUE = Constants.API_VERSION_MEDIA_TYPE + ".v2+" + Constants.JSON_MIME_TYPE;
    public static final MediaType V2_JSON = MediaType.valueOf(V2_JSON_VALUE);
}

然而,这似乎引发了以下错误,

WARN 37805 --- [  XNIO-2 task-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/vnd.test.onboarding.1.0+json' not supported

卷曲我正在使用,

curl -X POST "http://localhost:8080/api/authenticate" -H "Accepts: application/vnd.test.onboarding.1.0+json" -H "Content-Type: application/vnd.test.onboarding.1.0+json" -d '{"usernme":"test", "password":"password"}'

{
  "status" : 415,
  "error" : "Unsupported Media Type",
  "exception" : "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message" : "Unsupported Media Type",
  "path" : "/api/authenticate"
}

我不确定自定义媒体类型导致HttpMediaTypeNotSupportedException的原因。会不会认为Spring Boot应该能够自动处理这个问题。

更新

注册了消息转换器,

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setSupportedMediaTypes(Arrays.asList(Version.V1_JSON));
    jsonConverter.setSupportedMediaTypes(Arrays.asList(Version.V2_JSON));
    return jsonConverter;
}

1 个答案:

答案 0 :(得分:1)

不确定我是如何错过这一点但事实证明我请求的版本和Accept标题不正确。

应该是,

curl -X POST "http://localhost:8080/api/authenticate" -H "Accept: application/vnd.test.onboarding.1v+json" -H "Content-Type: application/vnd.test.onboarding.1v+json" -d '{"usernme":"test", "password":"password"}'

我还必须按如下方式指定内容协商,

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(true)
            .favorParameter(false)
            .ignoreAcceptHeader(false)
            .useJaf(false)
            .defaultContentType(MediaType.APPLICATION_JSON);
}

我不需要mappingJackson2HttpMessageConverter