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

ControllerAdvice are ignored and unexpected XML encoding #55

Open
zifnab87 opened this issue Jan 4, 2018 · 3 comments
Open

ControllerAdvice are ignored and unexpected XML encoding #55

zifnab87 opened this issue Jan 4, 2018 · 3 comments

Comments

@zifnab87
Copy link

zifnab87 commented Jan 4, 2018

Hi!

This seems to be the most elegant way to programmatically change json views based on user roles! Thank you for your work!

Unfortunately as soon as I add

    @Bean
    public JsonViewSupportFactoryBean views() {
        return new JsonViewSupportFactoryBean();
    }

my @ControllerAdvice classes stop being applied.

e.g I have a controllerAdvice that makes all the responses look like { content: [ ] } even if they are single objects and I definitely need those to be working :)

On top of that for some reason any end-point that has data that doesn't go through code like that:

return json.use(JsonView.with(userList)
                .onClass(PlatformUser.class, Match.match()
                        .exclude("password")
                )).returnValue();

is sending back XML! Even if it is a Hibernate Page object {content: [], total: [], pagination: { } } it is converted to XML.. while this was not needed in the past I had to add Accept headers in all the client requests to stop this from happening and have all of those as JSON again.

I am using Spring 5:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

From what I can tell it has to do with this part of code:

class JsonViewSupportFactoryBean {
[..]
private void decorateHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        List<HttpMessageConverter<?>> converters = new ArrayList(this.adapter.getMessageConverters());
        converters.add(this.converter);
        Iterator var3 = handlers.iterator();

        while(var3.hasNext()) {
            HandlerMethodReturnValueHandler handler = (HandlerMethodReturnValueHandler)var3.next();
            int index = handlers.indexOf(handler);
            if (handler instanceof HttpEntityMethodProcessor) {
                handlers.set(index, new JsonViewHttpEntityMethodProcessor(converters));
            } else if (handler instanceof RequestResponseBodyMethodProcessor) {
                // the following replaces the handler that used to contain controllerAdvices
                handlers.set(index, new JsonViewReturnValueHandler(converters, this.defaultView)); 
                break;
            }
        }

    }
[..]
}

image

Thanks!

@zifnab87 zifnab87 changed the title ControllerAdvice conflict and unexpected XML encoding ControllerAdvice are ignored and unexpected XML encoding Jan 4, 2018
zifnab87 added a commit to zifnab87/json-view that referenced this issue Jan 18, 2018
zifnab87 added a commit to zifnab87/json-view that referenced this issue Jan 18, 2018
@amit-gshe
Copy link

@zifnab87 I have encountered this issue and could you please create a merge request.

@zifnab87
Copy link
Author

zifnab87 commented Feb 8, 2019

@amit-gshe I didn't create a pull request since what I did is really messy and hacky - here is the diff from my old fork: master...zifnab87:master

@ashalathas
Copy link

Hi there, I am also facing this issue. After json view is executed, @controlleradvice logic is not called. Please provide fix asap

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