Skip to content
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

JsonViewSupportFactoryBean replaces all other converters? #30

Closed
aabeling opened this issue Aug 25, 2016 · 4 comments
Closed

JsonViewSupportFactoryBean replaces all other converters? #30

aabeling opened this issue Aug 25, 2016 · 4 comments

Comments

@aabeling
Copy link

aabeling commented Aug 25, 2016

Hi,

is it right that the JsonViewSupportFactoryBean replaces all other converters in
https://github.com/monitorjbl/json-view/blob/master/spring-json-view/src/main/java/com/monitorjbl/json/JsonViewSupportFactoryBean.java#L33 ?

Then in
https://github.com/monitorjbl/json-view/blob/master/spring-json-view/src/main/java/com/monitorjbl/json/JsonViewSupportFactoryBean.java#L40
the converter is added again?!

How about just replacing a possibly existing MappingJackson2HttpMessageConverter instead?
Now my application misses the Jaxb2RootElementHttpMessageConverter which it had before.

Thanks for this work so far.
Best regards
Achim

@tadas-subonis
Copy link

Ran into the same problem. I've removed line

adapter.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));

and it fixed my issues and it seems that it didn't break anything else.

@tadas-subonis
Copy link

Now the (our internal/custom) class looks:

package com.monitorjbl.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class JunoJsonViewSupportFactoryBean implements InitializingBean {

    @Autowired
    private RequestMappingHandlerAdapter adapter;
    private final JsonViewMessageConverter converter;

    public JunoJsonViewSupportFactoryBean() {
        this(new ObjectMapper());
    }

    public JunoJsonViewSupportFactoryBean(ObjectMapper mapper) {
        this.converter = new JsonViewMessageConverter(mapper.copy());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(adapter.getReturnValueHandlers());
        decorateHandlers(handlers);
        adapter.setReturnValueHandlers(handlers);
    }

    private void decorateHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        List<HttpMessageConverter<?>> converters = new ArrayList<>(adapter.getMessageConverters());
        converters.add(converter);
        for (HandlerMethodReturnValueHandler handler : handlers) {
            int index = handlers.indexOf(handler);
            if (handler instanceof HttpEntityMethodProcessor) {
                handlers.set(index, new JsonViewHttpEntityMethodProcessor(converters));
            } else if (handler instanceof RequestResponseBodyMethodProcessor) {
                handlers.set(index, new JsonViewReturnValueHandler(converters));
                break;
            }
        }
    }

}

@draas1
Copy link

draas1 commented Oct 10, 2016

I too had the same problem (and fix).

To me, the offending line mentioned above seems like a mistake. If removing that line breaks functionality in some way, the please let me know.

monitorjbl pushed a commit that referenced this issue Oct 29, 2016
Fix for #30, only replacing MappingJackson2HttpMessageConverter
Fix for #37, allowing a DefaultView setting on the JsonViewSupportFactoryBean
@monitorjbl
Copy link
Owner

Pushed a fix to the latest snapshot version, would you mind giving that a try? If you don't have any issues, I'll release it to Maven Central.

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

4 participants