4

I'm trying to bind a name to a filter in JAX-RS so I can secure some methods in the rest service as the following:

Secured Name Binding:

@NameBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Secured {
}

Authentication Filter:

@Secured
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationAgent implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
         //do something
    }
}

However, eclipse is giving me this error when I'm adding the secured annotation to my filter.

There is no JAX-RS application, resource or resource method with this name binding annotation.

enter image description here

fareed
  • 3,034
  • 6
  • 37
  • 65
  • Pretty smart editor. Name Binding should only be used when you want to limit the filter to resources classes/methods also annotated with the name binding annotation. If this is the case, then annotate the classes/methods you wan to go through that filter. If you want everything to go through the filter, then forget the annotation altogether. Just get rid of it – Paul Samsotha Nov 16 '16 at 11:43
  • @peeskillet I'm impressed! I don't know if this is smart or stupid. Thanks for this. Post that as an answer! – fareed Nov 16 '16 at 11:46
  • 1
    @peeskillet I only needed to bind it to any method/class to make it work. I stopped the moment it gave me the error. Its more of a warning than an error to me – fareed Nov 16 '16 at 11:48

2 Answers2

3

These types of error are not really big errors. For this type of error on JAX-RS we can mark it as a warning or ignore it at all.

For Eclipse, go to Window > Preferences > Jboss Tools > JAX-RS > JAX-RS Validator > JAX-RS Name Bindings and set Missing @Retention annotation to something other than "Error". (Yes, the preference name is misleading).

(To customize it for a specific project, click on Configure Project Specific Settings... in the top right corner)

bruno
  • 2,213
  • 1
  • 19
  • 31
  • What specific validation message should be configured as 'Warning' in the "JAX-RS Validator" dialog? – bruno Sep 28 '18 at 17:42
2

It's not really an error that will stop JAX-RS from working. It's more of just a warning (specific to that editor).

Name Binding should only be used when you want to limit the filter to resources classes/methods also annotated with the name binding annotation. If this is the case, then annotate the classes/methods you want to go through that filter. If you want everything to go through the filter, then forget the annotation altogether. Just get rid of it.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720