title: Defining a resource assembler for a REST Spring HATEOAS controller
body: <p>I'm trying to add HATEOAS links to a JSON resource served by a Spring REST controller.</p>
  
  <p>I see I should use a resource assembler as described at <a href="https://github.com/spring-projects/spring-hateoas" rel="noreferrer">https://github.com/spring-projects/spring-hateoas</a></p>
  
  <p>The example displays a Person class and a PersonResource class.</p>
  
  <p>I understand the PersonResource class is defined as:</p>
  
  <pre><code>public class PersonResource extends ResourceSupport {
  }
  </code></pre>
  
  <p>What is then the Person class ? Is it a data domain class ?</p>
  
  <p>In my case, I have defined an Admin class that is a REST domain class, and I specified it as having resource support:</p>
  
  <pre><code>public class Admin extends ResourceSupport {
  
      private String firstname;
      private String lastname;
      private String email;
      private String login;
      private String password;
      private String passwordSalt;
  
      public Admin() {
      }
  
      public String getFirstname() {
          return this.firstname;
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public String getLastname() {
          return this.lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getLogin() {
          return this.login;
      }
  
      public void setLogin(String login) {
          this.login = login;
      }
  
      public String getPassword() {
          return this.password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public String getPasswordSalt() {
          return passwordSalt;
      }
  
      public void setPasswordSalt(String passwordSalt) {
          this.passwordSalt = passwordSalt;
      }
  
      public EventAdmin toEventAdmin() {
          EventAdmin eventAdmin = new EventAdmin();
  
          BeanUtils.copyProperties(this, eventAdmin);
  
          return eventAdmin;
      }
  
      public static Admin fromEventAdmin(EventAdmin eventAdmin) {
          Admin admin = new Admin();
  
          BeanUtils.copyProperties(eventAdmin, admin);
  
          return admin;
      }
  
  }
  </code></pre>
  
  <p>My REST controller sees only this Admin class as it is a REST domain class. It does not know, and should not know, of anything data domain class.</p>
  
  <p>So I wonder how to use the resource assembler support here.</p>
  
  <p>I don't understand why I should have an additional data domain Admin class here.</p>
  
  <p>kind Regards,</p>
  
  <p>Following Mike's answer here is how my controller now looks like:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, produces = "application/json; charset=utf-8")
  @ResponseBody
  public ResponseEntity&lt;Admin&gt; add(@RequestBody Admin admin, UriComponentsBuilder builder) {
      AdminCreatedEvent adminCreatedEvent = adminService.add(new CreateAdminEvent(admin.toEventAdmin()));
      HttpHeaders responseHeaders = new HttpHeaders();
      responseHeaders.add("Content-Type", "application/json; charset=utf-8");
      responseHeaders.setLocation(builder.path("/admin/{id}").buildAndExpand(adminCreatedEvent.getAdminId()).toUri());
      Admin createdAdmin = adminResourceAssembler.toResource(adminCreatedEvent.getEventAdmin());
      ResponseEntity&lt;Admin&gt; responseEntity = new ResponseEntity&lt;Admin&gt;(createdAdmin, responseHeaders, HttpStatus.CREATED);
      return responseEntity;
  }
  </code></pre>
  
  <p>Before, instead of using the resource assembler I was doing a:</p>
  
  <pre><code>Admin createdAdmin = Admin.fromEventAdmin(adminCreatedEvent.getEventAdmin());
  createdAdmin.add(linkTo(methodOn(AdminController.class).add(createdAdmin, builder)).withSelfRel());
  </code></pre>
  
  <p>But it was not giving me the resource id in the url.</p>