Spring 引导 - RESTful 控制器、hateoas 和 JSON

Spring Boot - RESTful controllers, hateoas and JSON

所以我浏览了一些关于使用 spring hateoas(通过 spring-boot)的文档和示例。因此,通过以下示例,我创建了 2 个控制器。

这是第一个片段:

@RestController
@RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public HttpEntity<Resources<Resource<UserResponse>>> findAll() {
        List<UserResponse> userList = userService.finaAll();

        List<Resource<UserResponse>> resources = Lists.newArrayList();
        for (UserResponse user : userList) {
            Resource<UserResponse> userResource = new Resource<UserResponse>(user);
            resources.add(userResource);
            userResource.add(linkTo(methodOn(UserController.class).findAll()).withSelfRel());
            userResource.add(linkTo(methodOn(UserController.class).findById(user.getId())).withRel("viewUser"));
        }

        return new ResponseEntity(new Resources(resources), HttpStatus.OK);
    }
}

这是 UserResponse DTO:

public class UserResponse {

    private Long id;

    private String firstName;

    private String lastName;

    private String socialNumber;

    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate dateOfBirth;
}

Getter 和 setter 为简洁起见被删除,唯一值得一提的是 ID 属性的 getter/setter 上有 @JsonIgnore 和 @JsonProperty。

现在我得到的回复如下:

{
  "_embedded" : {
    "userResponses" : [ {
      "firstName" : "Brand",
      "lastName" : "White",
      "socialNumber" : "342asd3423",
      "dateOfBirth" : "1987-04-04",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users"
        },
        "viewUser" : {
          "href" : "http://localhost:8080/users/10"
        }
      }
    }, {
      "firstName" : "Snow",
      "lastName" : "Green",
      "socialNumber" : "3423cxvx423",
      "dateOfBirth" : "1987-01-12",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users"
        },
        "viewUser" : {
          "href" : "http://localhost:8080/users/11"
        }
      }
    } ]
  }
}

问题一:根据 http://docs.spring.io/spring-hateoas/docs/current/reference/html/ 格式应该是这样的:

links : [ { rel : "self", href : "http://myhost/people" } ] } in my example it looks like like "rel" is an attribute.

我有第二个控制器,基本上是复制粘贴的:

@RestController
@RequestMapping(value = "/users/{userId}/details", produces = MediaType.APPLICATION_JSON_VALUE)
public class DetailsController {
    //... services ...
    @RequestMapping(method = RequestMethod.GET)
    public HttpEntity<Resources<Resource<DetailsResponse>>> findAllUserDetails(@PathVariable("userId") Long userId) {
        Iterable<UserDetails> details = userDetails.findAll();

        List<Resource<DetailsResponse>> hyperList = Lists.newArrayList();

        for (UserDetails detail : details) {
            Resource<DetailsResponse> hyperRes = new Resource<DetailsResponse>(new DetailsResponse(details));
            hyperRes.add(linkTo(DetailsController.class, userId).withSelfRel());
            hyperRes.add(linkTo(DetailsController.class, userId).slash("/" + detail.getId()).withRel("viewDetail"));
            hyperRes.add(linkTo(DetailsController.class, userId).slash("/" + detail.getId()).withRel("updateDetail"));

            hyperList.add(hyperRes);
        }

        return new ResponseEntity(hyperList, HttpStatus.OK);
    }
}

而这个单一方法产生的响应是这样的:

{
    "lifespanDays": 60,
    "status": "deceased",
    "dateBorn": [
        2015,
        4,
        5
    ],
    "dateDied": [
        2015,
        6,
        4
    ],
    "links": [
        {
        "rel": "self",
        "href": "http://localhost:8080/users/10/details"
        },
        {
        "rel": "viewDetail",
        "href": "http://localhost:8080/users/10/details/30"
        },
        {
        "rel": "updateDetail",
        "href": "http://localhost:8080/users/10/details/30"
        }
    ]
}

第二个控制器的 DTO:

public class UserDetailResponse {

    private Long lifespanDays;

    private String status;

    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate dateBorn;

    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate dateDied;
}

我不明白 JSON 哪种格式是正确的?而且我也不明白为什么它们会有所不同,尽管在类型上指定的 produces MediaType 对两者都是 application/json 。另外,UserDetail 响应中的日期格式也发生了变化....

两个控制器都在同一个包下,都有相同的注释。这一切都是开箱即用的 Spring-使用一堆 stater 依赖项启动:

compile('org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE')
compile('org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE')
compile('org.springframework.boot:spring-boot-starter-data-rest:1.2.2.RELEASE') { transitive = true; }
compile('com.google.code.gson:gson:2.3.1');
compile('org.springframework.boot:spring-boot-starter-data-jpa:1.2.2.RELEASE') { transitive = true; };
compile('com.google.guava:guava:18.0')
compile('commons-beanutils:commons-beanutils:1.9.2')
runtime('org.hsqldb:hsqldb:2.3.2');

问题一:

Spring 引导中的默认响应格式是 HAL,这就是您得到的。链接的序列化确实与您预期的不同。 Spring HATEOAS 为 Jackson 注册了一个负责此操作的模块。顺便说一句:文档没有明确说明链接的呈现。

问题二:

OP 在评论中回答了。

FWIW:两种回答都是正确的 JSON。