10

我正在尝试将 HATEOAS 链接添加到 Spring REST 控制器提供的 JSON 资源。

我看到我应该使用https://github.com/spring-projects/spring-hateoas中描述的资源组装器

该示例显示了一个 Person 类和一个 PersonResource 类。

我了解 PersonResource 类定义为:

public class PersonResource extends ResourceSupport {
}

那么 Person 类是什么?它是一个数据域类吗?

在我的例子中,我定义了一个 Admin 类,它是一个 REST 域类,并将它指定为具有资源支持:

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;
    }

}

我的 REST 控制器只看到这个 Admin 类,因为它是一个 REST 域类。它不知道也不应该知道任何数据域类。

所以我想知道如何在这里使用资源汇编器支持。

我不明白为什么我应该在这里有一个额外的数据域管理类。

亲切的问候,

跟随迈克的回答是我的控制器现在的样子:

@RequestMapping(method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody
public ResponseEntity<Admin> 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<Admin> responseEntity = new ResponseEntity<Admin>(createdAdmin, responseHeaders, HttpStatus.CREATED);
    return responseEntity;
}

之前,我没有使用资源汇编器,而是在执行以下操作:

Admin createdAdmin = Admin.fromEventAdmin(adminCreatedEvent.getEventAdmin());
createdAdmin.add(linkTo(methodOn(AdminController.class).add(createdAdmin, builder)).withSelfRel());

但它没有给我 url 中的资源 id。

4

1 回答 1

9

您的ResourceAssembler实现需要了解数据域类和 REST 域类,因为它的工作是将前者转换为后者。

如果您想将数据类的知识保留在控制器之外,您可以创建一个资源转换服务,该服务将从存储库中检索数据并使用 aResourceAssembler将其转换为控制器可以知道的资源。

@Component
public class AdminResourceAssembler extends ResourceAssemblerSupport<Admin, AdminResource> {
    public AdminResourceAssembler() {
        super(AdminController.class, AdminResource.class);
    }

    public AdminResource toResource(Admin admin) {
        AdminResource adminResource = createResourceWithId(admin.getId(), admin); // adds a "self" link
        // TODO: copy properties from admin to adminResource
        return adminResource;
    }
}

@Service
public class AdminResourceService {
    @Inject private AdminRepository adminRepository;
    @Inject private AdminResourceAssembler adminResourceAssembler;

    @Transactional
    public AdminResource findOne(Long adminId) {
        Admin admin = adminRepository.findOne(adminId);
        AdminResource adminResource = adminResourceAssembler.toResource(admin);
        return adminResource;
    }
}

@Controller
@RequestMapping("/admins")
public class AdminController {
    @Inject private AdminResourceService adminResourceService;

    @RequestMapping(value="/{adminId}", method=RequestMethod.GET)
    public HttpEntity<AdminResource> findOne(@PathVariable("adminId") Long adminId) {
        AdminResource adminResource = adminResourceService.findOne(adminId);
        return new ReponseEntity<>(adminResource, HttpStatus.OK);
    }
}
于 2013-11-18T19:28:27.653 回答