Spring returns Resource in pure JSON not in HAL Format when including spring data rest
Asked Answered
H

1

5

When I use the default controller for my Entities, provided by Spring Data Rest everything works like it should. The output looks like this:

{
  "_links" : {
    "search" : {
      "href" : "http://localhost:8080/users/search"
    }
  },
  "_embedded" : {
    "users" : [ {
      "firstName" : "Max",
      "lastName" : "Mustermann",
      "email" : "[email protected]",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/myadmin"
        }
      }
    } ]
  }
}

But if I use my own Controller the output looks like this:

[ {
  "firstName" : "Max",
  "lastName" : "Mustermann",
  "email" : "[email protected]",
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:8080/user/myadmin"
  } ]
} ]

My Controller

@RestController
@RequestMapping("/user")
@EnableHypermediaSupport(type = {HypermediaType.HAL})
public class UserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping(method=RequestMethod.GET)
    public HttpEntity<List<User>> getUsers(){
        ArrayList<User> users = Lists.newArrayList(userRepository.findAll());
        for(User user : users){
            user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
        }
        return new ResponseEntity<List<User>>(users, HttpStatus.OK);
    }

    @RequestMapping(value="/{username}", method= RequestMethod.GET)
    public HttpEntity<User> getUser(@PathVariable String username){
        User user = userRepository.findByUsername(username);
        user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }
}

My User:

@Entity
@Table(name="users")
public class User extends ResourceSupport{
    @Id
    private String username;

    private String firstName;
    private String lastName;

    @JsonIgnore
    private boolean enabled;

    @JsonIgnore
    private String password;

    @Column(unique =  true)
    private String email;

    public User(){
        enabled = false;
    }
    //Getters and Setters
}

if i delete the spring data rest dependency and include spring-hateoas, spring-plugin-core and json-path (com.jayway.jsonpath) it works.. But i want to use spring-data-rest for some other entities

Two questions:

  1. Why isn't HAL the default with spring data rest included?
  2. How is it possible to set HAL as output format
Hire answered 20/2, 2015 at 22:55 Comment(0)
A
3

You need to return a subtype of ResourceSupport (usually Resource or Resources) to let the HAL Jackson converter kick in.

Also, @EnableHypermediaSupport has to be used on a JavaConfig configuration class, not the controller.

Amidase answered 21/2, 2015 at 7:58 Comment(6)
Hi, in the example i've seen here: spring.io/guides/gs/rest-hateoas the User have to extend ResourceSupport. Is this correct? I added the User to my example.Suburbanite
I've added an observation on @EnableHypermediaSupport.Amidase
@YannicKlem You need to return a subtype of ResourceSupport also means that you cannot return a plain List.Nutritive
@zeroflagL What will be the solution for returning a collection? Create an own collection which extends ResourceSupport?Suburbanite
@YannicKlem You can wrap a List into a Resources object: new Resources<User>(users)Nutritive
Thanks for fast responses. I will try that when I'm at homeSuburbanite

© 2022 - 2024 — McMap. All rights reserved.