day0ops
day0ops

Reputation: 7482

Custom media type in Spring Boot REST causes HttpMediaTypeNotSupportedException

Im using Spring Boot 1.5.2 and I have a simple REST controller as follows,

@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) {
    }
}

Constants,

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() {
    }
}

Version,

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);
}

However, this seems to be throwing the following error,

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 I'm using,

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"
}

I'm not sure why the custom media type causes the HttpMediaTypeNotSupportedException. Would've thought Spring Boot should be able to handle this automatically.

Updated

Message converter registered,

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

Upvotes: 2

Views: 4565

Answers (1)

day0ops
day0ops

Reputation: 7482

Not sure how I have missed this but it turns out the version and Accept header i was requesting was incorrect.

It should be,

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"}'

I also had to specify the content negotiation as follows,

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

and I did not need the mappingJackson2HttpMessageConverter

Upvotes: 2

Related Questions