Skip to content

Adding new HttpMessageConverters does not work #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Oni1 opened this issue Oct 17, 2017 · 9 comments
Closed

Adding new HttpMessageConverters does not work #654

Oni1 opened this issue Oct 17, 2017 · 9 comments

Comments

@Oni1
Copy link

Oni1 commented Oct 17, 2017

I'm developing a HAL-based REST API with spring-boot and spring-hateoas. I have a bean in my configuration class, which adds a new ByteArrayHttpMessageConverter, because my Controller returns a ByteArrayResource. When i debug the application and end up in the AbstractMessageConverterMethodProcessor class, i can't see my ByteArrayHttpMessageConverter in the messageConverters list.
spring-hateoas

My config file looks like this:

@Configuration
public class MyConfig{
    private static final String SPRING_HATEOAS_OBJECT_MAPPER = "_halObjectMapper";
    
    @Autowired
    @Qualifier(SPRING_HATEOAS_OBJECT_MAPPER)
    private ObjectMapper springHateoasObjectMapper;
    
    @Autowired
    private Jackson2ObjectMapperBuilder springBootObjectMapperBuilder;

    @Bean(name = "objectMapper")
    @Primary
    ObjectMapper objectMapper()
    {
        this.springBootObjectMapperBuilder.configure(this.springHateoasObjectMapper);
        return springHateoasObjectMapper;
    }
    
    @Bean
    public HttpMessageConverters customConverters() {
        ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
        arrayHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
        return new HttpMessageConverters(arrayHttpMessageConverter);
    }
    
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilder() {
        return builder -> builder.configure(springHateoasObjectMapper);
    }
}
´´´       

I'm using version 0.23.0.RELEASE of spring-hateoas.



@gregturn
Copy link
Contributor

Duplicates #118, #352, and #149.

@gregturn
Copy link
Contributor

Right now, Spring HATEOAS has a static-based configuration that has a BIG side effect of making dynamic updates like this tricky. The linked PR is a solution, but possibly not the best one. We're looking at tackling this in the new 1.0 branch in a more fluid, Spring fashion.

@Oni1
Copy link
Author

Oni1 commented Oct 17, 2017

@gregturn Which linked PR are you talking about? Thank you, for the answer.

@gregturn
Copy link
Contributor

Sorry, left out #618

@Oni1
Copy link
Author

Oni1 commented Oct 24, 2017

I couldn't solve my problem with the PR. I found a stackoverflow answer, what solved the problem. This should be better handled from spring-hateoas....

My code:

@EnableHypermediaSupport(type = HypermediaType.HAL)
@Configuration
public class ServerConfig extends WebMvcConfigurationSupport {
	
	@Autowired
	private RequestMappingHandlerAdapter adapter;
	
	@PostConstruct
	public void initStuff() {
	    List<HttpMessageConverter<?>> messageConverters = adapter.getMessageConverters();
	    ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
	    ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();
	    messageConverters.add(0, arrayHttpMessageConverter);
	    messageConverters.add(0, resourceHttpMessageConverter);
	}
}

@gregturn
Copy link
Contributor

The issue with using@EnableHypermediaSupport is you disable all the things supplied by Spring Boot and take it into yourself. So that isn't an optimal solution for most people.

It's an issue @olivergierke and I are looking into for a better developer experience.

@connorwyatt
Copy link

Is there any update on this? I'm currently having to use strings for my endpoints as I'm using value objects for all my ID path variables. Just wondering if there is a plan?

@gregturn
Copy link
Contributor

gregturn commented Feb 7, 2019

As we await the review of #728, have an work-in-progress that uses the newly built infrastructure to make it super simple to register your own media types.

@gregturn
Copy link
Contributor

gregturn commented Mar 5, 2019

Superseded by #833

@gregturn gregturn closed this as completed Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants