Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring引导-RESTful控制器、hateoas和JSON_Json_Spring Mvc_Spring Boot_Spring Hateoas - Fatal编程技术网

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

Spring引导-RESTful控制器、hateoas和JSON,json,spring-mvc,spring-boot,spring-hateoas,Json,Spring Mvc,Spring Boot,Spring Hateoas,因此,我阅读了一些关于使用SpringHateOAS(带SpringBoot)的文档和示例。通过下面的例子,我创建了两个控制器 以下是第一个示例的一个片段: @RestController @RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) public class UserController { @Autowired private UserService userSer

因此,我阅读了一些关于使用SpringHateOAS(带SpringBoot)的文档和示例。通过下面的例子,我创建了两个控制器

以下是第一个示例的一个片段:

@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);
    }
}
为了简洁起见,删除了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"
        }
      }
    } ]
  }
}
问题一:根据格式,应该是以下内容:

链接:[{rel:“self”,href:“}]} 在我的示例中,看起来“rel”是一个属性

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

@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);
    }
}
第二个控制器的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的正确格式是什么?我也不明白为什么它们不同,尽管在类型上指定的products 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');
问题一:

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

问题二:

由OP在评论中回答


FWIW:两个响应都是正确的JSON

谢谢,我注意到在第二段代码中,我没有在Resources类中包装资源列表,这就是为什么我得到了奇怪的响应。谢谢你向我介绍哈尔-非常感谢。
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;
}
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');