Skip to content

Including a custom Converter to the HATEOAS link converters #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dshunfen opened this issue Aug 30, 2017 · 3 comments
Closed

Including a custom Converter to the HATEOAS link converters #620

dshunfen opened this issue Aug 30, 2017 · 3 comments
Assignees

Comments

@dshunfen
Copy link

Having started used the HATEOAS Spring framework, I've found the ControllerLinkBuilder linkTo and methodOn methods to be very useful to link resources to one another.

However, since some controller classes may use an Enum as a PathVariable, one may want to include those Enum's in links by using their Enum value using a custom Converter, and not by their name. The default Enum -> String Converter uses the Enum's name to do this conversion.

Unfortunately, because of this referenced line, the HATEOAS spring framework creates a new DefaultFormattingConversionService and doesn't include Converters that are created as Bean's within an ApplicationContext or via DefaultFormattingConversionService.addFormatters.

Example
Creating a simple GET mapping with the following path:

@GetMapping(value = "/{myEnum}")
public ResponseEntity<ConfigPhaseType> get(@PathVariable("myEnum") final MyEnum myEnum) {
      return new ResponseEntity<>(myEnum, HttpStatus.OK);
}

And then creating a resource link to that path like this:

@GetMapping(value = "/")
public ResourceSupport getMyEnum() {
      ResourceSupport res = new ResourceSupport();
      res.add(linkTo(methodOn(EnumConverterExample.class).get(MyEnum.EXAMPLE)).withSelfRel());
}

Will always give:

links: [{
      rel: "self",
      href: "http://localhost:8080/api/EXAMPLE"
}]

in the HATEOAS link. As opposed to using the Enum "value", be it an int or String, etc. because of Spring HATEOAS always creating a new DefaultFormattingConversionService.

Could we mix in Converter classes on the ApplicationContext to the list of converters HATEOAS uses for links or at least provide some way to add them to the ConversionService explicitly?

private static final ConversionService CONVERSION_SERVICE = new DefaultFormattingConversionService();

@odrotbohm
Copy link
Member

Duplicates #118.

@odrotbohm
Copy link
Member

Thanks for reporting this. This is being worked on in the context of #618 (the original ticket being #118). Unsure how to resolve that as the static ControllerLinkBuilder limits our ability to pick up Spring MVC configuration.

@dshunfen
Copy link
Author

Thank you for getting back so quickly Oliver. Makes sense. I'll subscribe to #118 so I can track what is going on with these static helpers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants