How to register a Filter to one @RequestMapping method only?

membersound

I registered the following filter for general content logging:

@Bean
public Filter getLoggingFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setIncludeQueryString(true);
    filter.setIncludePayload(true);
    filter.setMaxPayloadLength(5120);
    return filter;
}

This filter catches on any request.

Now I'd like to apply this filter to only one specific @RequestMapping method.

Question: is that possible at all?

Update: the following filterbean results in a 404 when accessing the path.

@Bean
public FilterRegistrationBean getLoggingFilter() {
    FilterRegistrationBean filter = new FilterRegistrationBean(new CommonsRequestLoggingFilter());
    filter.addUrlPatterns("/rest/my/specific/method/*");
    return filter;
}
M. Deinum

You need a FilterRegistrationBean to specify URLs mappings. This fact is also mentioned in the reference guide.

@Bean
public Filter loggingFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setIncludeQueryString(true);
    filter.setIncludePayload(true);
    filter.setMaxPayloadLength(5120);
    return filter;
}

Next to the filter definition add the FilterRegistrationBean.

@Bean
public FilterRegistrationBean loggingFilterRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean(loggingFilter());
    registration.addUrlPatterns("/rest/my/specific/method/*");
    return registration;
}

This will attach the filter to the specified URL. Also be aware that the UrlPatterns are patterns inside the root URL. So if your application is mapped on /rest this filter won't be invoked.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to allow only one user to register with Stormpath

From Dev

How to ignore level @RequestMapping() and direct call method level @RequestMapping()?

From Dev

How to mock only one method using Sinon?

From Dev

How to make a method accessable to only one class?

From Dev

Spring MVC - How to register more than one ArgumentResolver and choose one dynamically in the Controller Method?

From Dev

MySQL Restrict user to register only one database

From Dev

MySQL Restrict user to register only one database

From Dev

How to filter object from one key only and return it as json?

From Dev

How can i get only one solution from filter map?

From Dev

How to add a filter only for one special path WebSecurityConfigurerAdapter

From Dev

How to filter object from one key only and return it as json?

From Dev

How to register EventBus only once

From Dev

How can i make program register only one key press with getch or different function?

From Dev

How to mock only one static method and test the other

From Dev

Django How to make only one query with queryset exists method?

From Dev

How to allow only one class to access a method from another class?

From Dev

How to expose property by JMS/Serializer for only one method?

From Dev

How to make setInterval method have only one instance running

From Dev

How can I register a filter in AngularJS?

From Dev

how to call request mapping method level through class level RequestMapping

From Dev

how to call request mapping method level through class level RequestMapping

From Dev

Is it possible to register two actions within one <intent-filter> for Activity

From Dev

The Model parameter in @RequestMapping method

From Dev

how to apply filter method on this

From Dev

moq only one method in a class

From Dev

How to register a signal handler as a class method?

From Dev

how to get register MBean with method getPlatformMXBean()?

From Dev

How to register multiple folders with one WatchService

From Dev

How to make sure that method is executed only once and from one thread only?

Related Related

  1. 1

    How to allow only one user to register with Stormpath

  2. 2

    How to ignore level @RequestMapping() and direct call method level @RequestMapping()?

  3. 3

    How to mock only one method using Sinon?

  4. 4

    How to make a method accessable to only one class?

  5. 5

    Spring MVC - How to register more than one ArgumentResolver and choose one dynamically in the Controller Method?

  6. 6

    MySQL Restrict user to register only one database

  7. 7

    MySQL Restrict user to register only one database

  8. 8

    How to filter object from one key only and return it as json?

  9. 9

    How can i get only one solution from filter map?

  10. 10

    How to add a filter only for one special path WebSecurityConfigurerAdapter

  11. 11

    How to filter object from one key only and return it as json?

  12. 12

    How to register EventBus only once

  13. 13

    How can i make program register only one key press with getch or different function?

  14. 14

    How to mock only one static method and test the other

  15. 15

    Django How to make only one query with queryset exists method?

  16. 16

    How to allow only one class to access a method from another class?

  17. 17

    How to expose property by JMS/Serializer for only one method?

  18. 18

    How to make setInterval method have only one instance running

  19. 19

    How can I register a filter in AngularJS?

  20. 20

    how to call request mapping method level through class level RequestMapping

  21. 21

    how to call request mapping method level through class level RequestMapping

  22. 22

    Is it possible to register two actions within one <intent-filter> for Activity

  23. 23

    The Model parameter in @RequestMapping method

  24. 24

    how to apply filter method on this

  25. 25

    moq only one method in a class

  26. 26

    How to register a signal handler as a class method?

  27. 27

    how to get register MBean with method getPlatformMXBean()?

  28. 28

    How to register multiple folders with one WatchService

  29. 29

    How to make sure that method is executed only once and from one thread only?

HotTag

Archive