Klasse SpringControllerLinkBuilder

java.lang.Object
de.kamillionlabs.hateoflux.linkbuilder.SpringControllerLinkBuilder

public class SpringControllerLinkBuilder extends Object
Provides functionality to build URI links pointing to Spring controllers. It is designed to work seamlessly with various Spring annotations such as RestController, GetMapping, PathVariable, RequestParam, etc. This class aims to be a direct replacement for the WebMvcLinkBuilder i.e. WebFluxLinkBuilder in Spring HATEOAS, offering a similar interface and usage.
  • Methodendetails

    • linkTo

      public static <ControllerT> Link linkTo(Class<ControllerT> controllerClass, ControllerMethodReference<ControllerT> methodRef)
      Creates a Link object that represents a link to the resource(s) addressed by calling the API of the specified controller class. This method uses the ControllerMethodReference to ensure type-safe referencing of controller methods. The link is expanded using the parameters in the template and the parameters with which the method reference was called.

      The method distinguishes between PathVariable and RequestParam (i.e., query parameters). When parameters of the latter type are used, collections are allowed. By default, collections are expanded in a non-composite way (i.e., var=1,2 as opposed to var=1&var=2). If the parameter in the controller class is annotated with Composite, then linkTo() will adhere to it, i.e., render it in the composite way.

      Example usage:
      @ signs are prepended with "_" because of a javadocs bug where the @ the beginning of a line is interpreted and thus messes up the code example

      Given the following class:

       _@Controller;
       _@RequestMapping("/user")
       public class UserController {
      
          _@GetMapping("/{userId}")
          public User getUser(@PathVariable userId){
          ...
          }
        }
       

      When the linkTo() method is called as follows:

       Link link = linkTo(UserController.class, c -> c.getUser("12345"));
       

      The resulting link has then the href: /user/12345

      Typparameter:
      ControllerT - the type of the controller
      Parameter:
      controllerClass - the class of the controller containing the target method. This class must be annotated with RestController or Controller to be valid.
      methodRef - a functional interface implementation that references the controller method for which the link is to be generated.
      Gibt zurück:
      an expanded Link object that encapsulates the URI pointing to the resource as exposed by the controller method referenced
      Löst aus:
      IllegalArgumentException - if the controller class is not correctly annotated as a Controller or RestController, which is necessary for the correct functioning of the method reference.
    • linkTo

      public static <ControllerT> Link linkTo(Class<ControllerT> controllerClass)
      Variation of the linkTo(Class, ControllerMethodReference) method. Please refer to the mentioned method for full documentation and usage examples. This method calls the aforementioned method with methodRef=null.
      Typparameter:
      ControllerT - the type of the controller
      Parameter:
      controllerClass - the class of the controller containing the target method. This class must be annotated with RestController or Controller to be valid.
      Gibt zurück:
      a Link object that encapsulates the URI pointing to the resource as exposed by the controller method referenced