Code Monkey home page Code Monkey logo

Comments (3)

Antibrumm avatar Antibrumm commented on August 29, 2024

Thanks for the info. I think I did not fall into this problem as I use an adapted version of the Readme. If you like you could give it a try and if that works for you too I will update the Readme?

/**
 * A specific {@link MappingJackson2HttpMessageConverter} that configures the given {@link ObjectMapper} with the
 * {@link AntPathFilterMixin}. This is needed only if we need multiple ObjectMappers.
 *
 * @author mfrey
 *
 */
public class AntPathFilterJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {

    public AntPathFilterJackson2HttpMessageConverter(final ObjectMapper objectMapper) {
        super(objectMapper);
        ObjectMapper copy = objectMapper.copy();
        copy.addMixIn(Object.class, AntPathFilterMixin.class);
        setObjectMapper(copy);
    }

    @Override
    public boolean canWrite(final Class<?> clazz, final MediaType mediaType) {
        return AntPathFilterMappingJacksonValue.class.isAssignableFrom(clazz);
    }
}
@Configuration
public class WebConfig extends DelegatingWebMvcConfiguration {
 @Override
    public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
        // Add a MappingJackson2HttpMessageConverter so that
        // objectMapper.writeFiltered
        // is using the objectMapper configured with the needed Mixin

        messageConverters.add(new AntPathFilterJackson2HttpMessageConverter(objectMapper));

        //
        addDefaultHttpMessageConverters(messageConverters);
        super.configureMessageConverters(messageConverters);
    }
}

 @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AntPathFilterMappingJacksonValue list() throws JsonProcessingException {
        List<User> allUsers = userService.allUsers();
        return new AntPathFilterMappingJacksonValue(allUsers, "id", "login", "firstName", "lastName", "email",
                "currentRoles", "managedScopes.id", "managedScopes.label", "active");
    }

from jackson-antpathfilter.

mpscheidt avatar mpscheidt commented on August 29, 2024

That works fine! Thanks a lot for that.

Your project is very useful. I wonder why the Jackson library hasn't come up with such a feature yet.

A suggestion is to make the web config slightly more lightweight (so you don't need to explicitely deal with default message converters and super calls) by implementing the WebMvcConfigurer interface (instead of extending DelegatingWebMvcConfiguration):

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		converters.add(0, new AntPathFilterJackson2HttpMessageConverter(objectMapper));
	}
}

Apparently the AntPathFilterJackson2HttpMessageConverter needs to be added at the beginning at the list of message converters, in order to prevent the other message converters from feeling responsible before AntPathFilterJackson2HttpMessageConverter gets its chance.

from jackson-antpathfilter.

Antibrumm avatar Antibrumm commented on August 29, 2024

Thanks for the feedback. Once i have time i update the readme.

I didnt know we can give the order of converters. This will simplify my setups alot :)

from jackson-antpathfilter.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.