Created
December 15, 2019 00:48
-
-
Save thiagofa/8952d7fee7650c94bb116917d63c9ae6 to your computer and use it in GitHub Desktop.
Resolve o problema de custom media types com Spring HATEOAS (com formato HAL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import javax.annotation.PostConstruct; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.hateoas.MediaTypes; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.converter.HttpMessageConverter; | |
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; | |
// Fonte: https://github.com/spring-projects/spring-hateoas/issues/263#issuecomment-358969098 | |
@Component | |
public class HalCustomMediaTypeEnabler { | |
private final RequestMappingHandlerAdapter requestMappingHandlerAdapter; | |
@Autowired | |
public HalCustomMediaTypeEnabler(RequestMappingHandlerAdapter requestMappingHandlerAdapter) { | |
this.requestMappingHandlerAdapter = requestMappingHandlerAdapter; | |
} | |
@PostConstruct | |
public void enableVndHalJson() { | |
for (HttpMessageConverter<?> converter : requestMappingHandlerAdapter.getMessageConverters()) { | |
if (converter instanceof MappingJackson2HttpMessageConverter | |
&& converter.getSupportedMediaTypes().contains(MediaTypes.HAL_JSON)) { | |
MappingJackson2HttpMessageConverter messageConverter = (MappingJackson2HttpMessageConverter) converter; | |
messageConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON, | |
MediaType.valueOf("application/custom-media-type-aqui"))); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment