json-view icon indicating copy to clipboard operation
json-view copied to clipboard

ControllerAdvice are ignored and unexpected XML encoding

Open zifnab87 opened this issue 8 years ago • 3 comments

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 avatar Jan 04 '18 18:01 zifnab87

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

amit-gshe avatar Sep 07 '18 03:09 amit-gshe

@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: https://github.com/monitorjbl/json-view/compare/master...zifnab87:master

zifnab87 avatar Feb 08 '19 06:02 zifnab87

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

ashalathas avatar Aug 31 '21 12:08 ashalathas