2

If I use @JsonView on a POJO and on a controller in Spring, my HATEOAS links are hidden. I can understand why this happens, because the _links property isn't annotated with the correct view class, but it's not the behaviour I need in this case. Is there a way to always include the _links property, regardless of using a view class or not?

My POJO looks something like:

@Entity(name = "groups")
@SequenceGenerator(name = "groups_groupid_seq", sequenceName = "groups_groupid_seq")
public class Group extends ResourceSupport {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "groups_groupid_seq")
    @JsonView(Views.Summary.class)
    private long groupID;

    @JsonView(Views.Full.class)
    private String name;

    /*
        Getters & setters ...
     */
}

And my controller looks something like:

@RestController
@RequestMapping(path = "/group")
@ExposesResourceFor(Group.class)
public class GroupApiController {
    @JsonView(Views.Summary.class)
    @RequestMapping(path = "/", method = RequestMethod.GET)
    public Iterable getPermittedGroups(
            Authentication authentication) {
        // load groups...
    }
}

My HATEOAS configuration:

@Configuration
@EnableAspectJAutoProxy
@EnableEntityLinks
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class HateoasConfig {
    //
}

I don't have to use @JsonView, so I'm happy to use an alternative. But I do need to be able to return different views on the same class, from different controller methods, otherwise I could use @JsonIgnore.

delucasvb
  • 5,393
  • 4
  • 25
  • 35

1 Answers1

2

Is there a way to always include the _links property, regardless of using a view class or not?

Using Spring Boot
Set the spring jackson mapper configuration spring.jackson.mapper.default-view-inclusion=true Within your application.properties

Using Spring @Configuration Classes
This topic is well covered in "Configuring Object Mapper in Spring"

However, there are currently issues in particular with HATEOAS that prevent this from working. In particular is the open ticket "Support for HATEOAS-Links in Json-Views"

Community
  • 1
  • 1