Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在HATEOAS\u嵌入式PagedResource中命名Spring页面键_Java_Spring_Spring Boot_Spring Hateoas - Fatal编程技术网

Java 在HATEOAS\u嵌入式PagedResource中命名Spring页面键

Java 在HATEOAS\u嵌入式PagedResource中命名Spring页面键,java,spring,spring-boot,spring-hateoas,Java,Spring,Spring Boot,Spring Hateoas,我们目前正在最新的项目中实现一个API库。 我们正在尝试使用SpringHateoas和HAL作为适当的库来生成json HAL响应 使用Spring boot 1.2.5.RELEASE+提供的Spring HATEOAS版本。 目前,我们正在映射要作为资源返回的实际JPA实体 即,我们的实体如下所示: import lombok.Getter; import lombok.Setter; @Getter @Setter @Entity @Table(name = "users") publ

我们目前正在最新的项目中实现一个API库。 我们正在尝试使用SpringHateoas和HAL作为适当的库来生成json HAL响应

使用Spring boot 1.2.5.RELEASE+提供的Spring HATEOAS版本。 目前,我们正在映射要作为资源返回的实际JPA实体

即,我们的实体如下所示:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "users")
public class User {

    @Id
    private UUID id;

    @Column(nullable = false, length = 70)
    private String firstName;

    @Column(nullable = false, length = 70)
    private String lastName;
}
我们的存储库:

public interface UserRepository extends PagingAndSortingRepository<User, UUID>, JpaSpecificationExecutor<User> {
    User findByUsername(String username);
}

据我所知,作者自己对这个问题的评论,找到的解决方案是将@Relation(collectionRelation=“users”)添加到User类中

...
@Table(name = "users")
@Relation(collectionRelation = "users")
public class User {
...

通过使用Spring HATEOAS包中的@关系解决了该问题。例如@Relation(collectionRelation=“users”)可以自由回答您自己的问题,并提供详细信息,使其对其他人有用!完美的对我来说,它是DTO类的一种符号。非常感谢。
@RestController
@RequestMapping(value = "/users", produces =MediaType.APPLICATION_JSON_VALUE) // this is done otherwise objectmapper returns xml for some reason
public UserRestController {

    public UserService userService;
    public EntityLinks entityLinks;

    @Autowired
    public UserRestController(UserService userService, EntityLinks entityLinks) {
        this.userService = userService;
        this.entityLinks = entityLinks;
    }

    @RequestMapping(method = RequestMethod.GET)
    public PagedResource<Resource<User>> getUsers(Pageable pageable, PagedResourcesAssembler<User> pagedResourcesAssembler) {
        Page<User> userPage = this.userService.findAll(pageable);

        PagedResources<Resource<User>> userPagedResources =
            pagedResourcesAssembler.toResource(
                    usersPage,
                    linkTo(methodOn(UserRestController.class).showUsers(pageable, pagedResourcesAssembler)).withSelfRel());

        return userPagedResources;

    }
}
{
  "_links": {
    "self": {
      "href": "http://localhost:8080/users{?page,size,sort}",
      "templated": true
    },
    "next": {
      "href": "http://localhost:8080/users?page=1&size=1&sort=firstname,lastname,asc"
    }
  },
  "_embedded": {
    "userList": [{
      "id": "2027bea9-cfdc-4724-b29c-39b3f64cbfd5",
      "firstname": "admin",
      "lastname": "asdf",
    }]
  },
  "page": {
    "size": 1,
    "totalElements": 2,
    "totalPages": 2,
    "number": 0
  }
}
{
  "_links": {
    "self": {
      "href": "http://localhost:8080/users{?page,size,sort}",
      "templated": true
    },
    "prev": {
      "href": "http://localhost:8080/users?page=0&size=1&sort=firstname,lastname,asc"
    }
  },
  "_embedded": {
    "user_$$_jvst163_9List": [{
      "id": "52c0c09e-c386-4aec-9723-f8beaf99adc5",
      "username": "admin",,
      "firstname": "firstname",
      "lastname": "lastname"
    }]
  },
  "page": {
    "size": 1,
    "totalElements": 2,
    "totalPages": 2,
    "number": 1
  }
}
...
@Table(name = "users")
@Relation(collectionRelation = "users")
public class User {
...