Spring HATEOAS具有嵌套资源和JsonView过滤

时间:2017-06-14 06:10:43

标签: java spring jackson hateoas json-view

我正在尝试使用Resource<>添加HATEOAS链接,同时使用@JsonView进行过滤。但是,我不知道如何将链接添加到嵌套对象。

on Github上的项目中,我已在this project上进行了扩展(添加了开放式拉取请求以使其无需嵌套资源),添加了&#34;字符&#34 ;具有嵌套User

的实体

访问~/characters/resource-filtered路线时,预计会嵌套User&#34;播放器&#34;与firstNmbioDetails字段一起显示,并且Spring生成了自己的链接,但没有userIdlastNm字段。

我的过滤工作正常,但我找不到符合ResourceAssembler范例的嵌套资源示例。似乎有必要使用ResourceAssembler@JsonView工作。

任何有助于协调这两个概念的帮助将不胜感激。如果您可以完全破解它,请考虑向我发送拉取请求。

User.java

//package and imports
...

public class User implements Serializable {

    @JsonView(UserView.Detail.class)
    private Long userId;

    @JsonView({ UserView.Summary.class, CharacterView.Summary.class })
    private String bioDetails;

    @JsonView({ UserView.Summary.class, CharacterView.Summary.class })
    private String firstNm;

    @JsonView({ UserView.Detail.class, CharacterView.Detail.class })
    private String lastNm;

    public User(Long userId, String firstNm, String lastNm) {
        this.userId = userId;
        this.firstNm = firstNm;
        this.lastNm = lastNm;
    }

    public User(Long userId) {
        this.userId = userId;
    }

    ...
    // getters and setters
    ...

}

CharacterModel.java

//package and imports
...

@Entity
public class CharacterModel implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonView(CharacterView.Summary.class)
    private Long characterId;

    @JsonView(CharacterView.Detail.class)
    private String biography;

    @JsonView(CharacterView.Summary.class)
    private String name;

    @JsonView(CharacterView.Summary.class)
    private User player;

    public CharacterModel(Long characterId, String name, String biography, User player) {
        this.characterId = characterId;
        this.name = name;
        this.biography = biography;
        this.player = player;
    }

    public CharacterModel(Long characterId) {
        this.characterId = characterId;
    }

    ...
    // getters and setters
    ...
}

CharacterController.java

//package and imports
...

@RestController
@RequestMapping("/characters")
public class CharacterController {

    @Autowired
    private CharacterResourceAssembler characterResourceAssembler;

    ...

    @JsonView(CharacterView.Summary.class)
    @RequestMapping(value = "/resource-filtered", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public Resource<CharacterModel> getFilteredCharacterWithResource() {
        CharacterModel model = new CharacterModel(1L, "TEST NAME", "TEST BIOGRAPHY", new User(1L, "Fred", "Flintstone"));
        return characterResourceAssembler.toResource(model);
    }

    ...
}

CharacterResourceAssembler.java

//package and imports
...

@Component
public class CharacterResourceAssembler implements ResourceAssembler<CharacterModel, Resource<CharacterModel>>{

    @Override
    public Resource<CharacterModel> toResource(CharacterModel user) {
        Resource<CharacterModel> resource = new Resource<CharacterModel>(user);
        resource.add(linkTo(CharacterController.class).withSelfRel());
        return resource;
    }

}

0 个答案:

没有答案