0

イム、カスタムメディアタイプが原因HttpMediaTypeNotSupportedException次のように春のブート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); 
} 

ただし、これは次のエラーを投げているようです。

カスタムメディアタイプが HttpMediaTypeNotSupportedExceptionを引き起こし、なぜ私が使用している
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" 
} 

は私はわかりません。 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; 
} 
+0

を必要としなかったあなたは、有効なメッセージコンバータを持っていますか登録された?アプリケーション/ jsonだけを使用するように変更すると動作しますか?あなたはjackson 2を使っていますか? – alfcope

+0

参照用にメッセージコンバータを追加しました。私はそれを間違ってしまった? – nixgadgets

+0

私が要求していたメディアタイプが正しくないことが分かります。私は答えに追加しました。なぜ私はこれを早く実現しなかったのか分かりません。 – nixgadgets

答えて

0

私はこれを見逃しているが、それはバージョンが判明し、私が要求した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

関連する問題