Skip to content

Hal rendering Jackson2HalModule for custom type is not working #1259

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

Open
yvoroshilov opened this issue Apr 5, 2020 · 7 comments
Open

Hal rendering Jackson2HalModule for custom type is not working #1259

yvoroshilov opened this issue Apr 5, 2020 · 7 comments

Comments

@yvoroshilov
Copy link

yvoroshilov commented Apr 5, 2020

Http converter renders data as regular json instead (i.e. "links" instead of "_links"). If set jackson module to collectionjson or uber everything seems to work fine.
Configuration

@Configuration
public class ContactDirMediaTypeConfiguration implements HypermediaMappingInformation {

  @Override
  public Module getJacksonModule() {
    return new Jackson2HalModule();
  }

  @Override
  public List<MediaType> getMediaTypes() {
    return MediaType.parseMediaTypes("application/vnd.contactdir.v1+json, application/hal+json");
  }
}

configuration provider

public class ContactDirMediaTypeConfigurationProvider implements MediaTypeConfigurationProvider {

  @Override
  public Class<? extends HypermediaMappingInformation> getConfiguration() {
    return ContactDirMediaTypeConfiguration.class;
  }

  @Override
  public boolean supportsAny(Collection<MediaType> mediaTypes) {
    return true;
  }
}

controller

@RestController
@RequestMapping("/api")
public class ContactListController {
  @GetMapping(path = "/contacts",
      produces = {"application/vnd.contactdir.v1+json", "application/hal+json"})
  public ResponseEntity<ContactDto> getAllContacts() {
    ContactDto dto = new ContactDto().setName("dfsfdsfsdf");
    dto.add(linkTo(ContactListController.class).withSelfRel());
    return new ResponseEntity<>(dto, HttpStatus.OK);
  }
}

Project reproducing the problem
Is it a bug or (what is more possible) did I miss something?

@gregturn
Copy link
Contributor

gregturn commented Apr 5, 2020

Did you see #1253 (comment)?

@yvoroshilov
Copy link
Author

Now I've read. Thank you very much, everything is working great now.

gregturn added a commit that referenced this issue Apr 5, 2020
By having this custom hypermedia type register ALL the parts of the HAL Jackson module, it provides a better example to users that wish to serve a custom media type that is HAL-like.

Related issues: #1259, #1253
@odrotbohm
Copy link
Member

While I think the route via a custom media type registration is a decent workaround, it's not what it was designed for initially and going down that route shouldn't be necessary if all you want to do is register an media type name alias to application/hal+json. As this thread proves, users would have to know all the delicate details of how to properly set up mappers and their moving parts. That's a bit much of the framework falling into one's face for a very simple task to achieve.

The concrete media types a media type implementation is registered under is already obtained via HypermediaMappingInformation.getMediaTypes(). Our HAL support's implementation HalMediaTypeConfiguration currently exposes a hard coded application/hal+json, but could also inspect the HalConfiguration (a bean customizable by the user) it already has available to pick up other media type names. User code could then just list the additional name(s) on that configuration object, and our infrastructure setup would pick that up and make sure the HttpMessageConverter that we register is triggered for that defined alias.

WDYT?

@odrotbohm odrotbohm reopened this Apr 6, 2020
@odrotbohm
Copy link
Member

I seem to get lost between tickets that are touching this topic. This comment was meant to comment on #1253.

@durimkryeziu
Copy link

I still have the issue of serializing links instead of _links?

@odrotbohm
Copy link
Member

I've checked the provided sample, and it's a bit of a mess. Contrary to what's described here, it only activates UBER but then apparently also tries to register a custom media type for HAL, but does that via an outdated way. We support registering custom media types via HalConfiguration.withMediaType(…). As that's what the ticket seems to ask for originally, I am inclined to close this as works as designed on recent versions of Spring HATEOAS.

@durimkryeziu
Copy link

I've checked the provided sample, and it's a bit of a mess. Contrary to what's described here, it only activates UBER but then apparently also tries to register a custom media type for HAL, but does that via an outdated way. We support registering custom media types via HalConfiguration.withMediaType(…). As that's what the ticket seems to ask for originally, I am inclined to close this as works as designed on recent versions of Spring HATEOAS.

Well, you're right, I was working with the wrong ObjectMapper, that's why I got links instead of _links, thanks for quick reply 🙌

Now I'm struggling with query params on Links 😬

#535 (comment)

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

No branches or pull requests

4 participants