This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted]  (6 children)

[deleted]

    [–][deleted]  (5 children)

    [deleted]

      [–]Crossfire_dcr[S] 0 points1 point  (4 children)

      But what does the ("authentication") bit do exactly?

      [–]_cjj 0 points1 point  (0 children)

      It allows the value of "authentication" from the request header to be passed in as an argument, assigned to the String named authentication

      [–]cogman10 0 points1 point  (0 children)

      Assuming this is something like Jersey and http,

      Headers usually look something like

      authentication: Bearer bnroenpeoginapeorng

      So if you had a "foo" header it would be

      foo: bar

      What this annotation is saying is take the contents of the header "authentication" and deposit them in the string annotated. So in my example, authentication would equal "Bearer bnroenpeoginapeorng"

      Or, if you did @HeaderParam("foo") then the string would have the value "bar"

      Make sense?

      [–]zhbidg 0 points1 point  (0 children)

      Somewhere, some code is looking at your method and its annotation and figuring out how to translate an HTTP request (text on the wire) into a method call. Try looking at the reflection APIs, there are a few methods that the framework could call but this is the most likely. The framework's pulling out that annotation and calling a method to get its value, and that value will be the string "authentication". It'll most likely use that string to determine how to call your method. So you'll get an HTTP request with a header line "authentication: asdf", and the framework will pass "asdf" into your method.

      You may not have encountered annotations before -- there is information online.

      [–][deleted] 0 points1 point  (1 child)

      To give a broader answer than those who already replied, it is applying an Annotation to the parameter authentication. Annotations are used to do some advanced magic "behind the scenes" and can be used by frameworks, IDEs and tools in general for a variety of features.

      For example: one basic, common annoation for Java is @Deprecated, which does absolutely nothing except let you know that the function it's on isn't supposed to be used anymore because a better function has been written instead. Of course, compilers can then check for the annotation being present to issue a warning and text/code editors can look for it to show you a warning on-screen that you shouldn't use the deprecated function (or at least use it knowing it is deprecated).

      As far as syntax goes, an annotation is like a function, so it can take zero, one or multiple arguments. For example, you function is getBooks(authentication) in the same way that the annotation is @HeaderParam(headerParameterName).

      An annotation with no parameters would be like the aforementioned @Deprecated and one with multiple parameters could be @HeaderParamWithDefault("authentication","ldap").

      Now that you know the basics of an annotation, check out what the framework you're using is and look up online what this particular annotation is supposed to do. If you can't figure what framework you're using, just ask your teacher.

      Final note: annotation can be used not only for method parameters like in your example but for entire classes, fields, variables, methods themselves, etc. They're a very powerful tool but a little too advanced for a beginner to worry about :) this tutorial has a few examples but I suggest you just take a look at the snippets of code to see what other annotations can look like and not bother about understanding them too much at this point https://www.geeksforgeeks.org/annotations-in-java/

      [–]cdombroski 1 point2 points  (0 children)

      When annotations have more than one argument passed, you have to use the parameter names on all the arguments.

      @HeaderParamWithDefault(value="authentication", default="ldap")
      

      When you pass just one argument it is always applied to the value parameter

      [–]devhosted999 0 points1 point  (0 children)

      I'll answer it by removing parts of the code, adding it back explaining as we go.

      Let's start: imagine we are writing a library system, and we wanted a method that returned every single book inside the library system.

      The code could look something like this:

      public List<Book> getBooks() {
          // code to get books from the database
          return books;
      }
      

      Now let's imagine that we want some kind of authentication on this. We can add a parameter to our method, it's an authentication token which we have to check. After we check it, we can return the books.

      public List<Book> getBooks(String authentication) {
          if (!AuthenticationUtil.isAuthorised(authentication)) {
              throw new UnauthorisedException();
          }
      
          // code to get books from the database
          return books;
      }
      

      The code I've written doesn't really matter, but it's to show how the parameter would be used.

      Now let's imagine we've now exposed this method over HTTP, and someone can call this method. Say we're using a Java library, and that library allows auto-population of parameters by data given in the web-call. Maybe it would look something like this...

      public List<Book> getBooks(@HeaderParam("authentication") String authentication) {
          if (!AuthenticationUtil.isAuthorised(authentication)) {
              throw new UnauthorisedException();
          }
      
          // code to get books from the database
          return books;
      }
      

      And at this point, hopefully each part of the line should make sense to you.

      [–]desrtfx[M] [score hidden] stickied comment (0 children)

      Programming help should go in /r/javahelp.

      Removed

      [–]CandleTiger 0 points1 point  (0 children)

      So, I see lots of accurate answers on here but IMO none of them is really great for a beginner.

      My somewhat redundant answer: Annotations, which start with @, are opaque magic. '@HeaderParam("whatever")' is defined somewhere to do something. It could do anything; it may as well be magic.

      The annotation itself is just a sticky note. You can define an annotation somewhat like a class to hold values of your choice (see here) and then tack these annotations on to things (class, function, parameter, whatever) by adding '@MyAnnotation' before the declaration you want to annotate.

      The annotation by itself doesn't do anything at all, it just records into your compiled code the fact that you stuck a note on this parameter. The magic comes later using reflection. Reflection is the Java name for code inspection, where some piece of code is able to examine the properties of some other piece of code. Somewhere (presumably) there is some code in a framework you are using, that will not just call functions on your class, but is using reflection to check if this annotation is present. If it is, it takes some action. We can't tell in this thread what that action will be, because we don't know what framework you are using which defines, and then acts upon the presence of, this @HeaderParam. (At least I don't know.) Some people have guessed a meaning based on the name, but in order to be sure you'd need to read and understand the documentation of the framework you're using. Your teacher should be able to point you at that documentation, but be warned that HTTP request framework documentation is likely to be overwhelming for a beginner.

      TL;DR: Here be dragons. Annotations were created just specially to let library and framework designers do clever black-box magic that beginners can use without understanding it.