gpt4 book ai didi

spring - 使用 Spring Data REST 公开枚举

转载 作者:行者123 更新时间:2023-12-03 03:06:59 30 4
gpt4 key购买 nike

我正在使用 Spring Boot 1.5.3、Spring Data REST、HATEOAS。我有一个简单的实体模型:

@Entity
public class User extends AbstractEntity implements UserDetails {
private static final long serialVersionUID = 5745401123028683585L;
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
@NotNull(message = "The name of the user cannot be blank")
@Column(nullable = false)
private String name;

/** CONTACT INFORMATION **/
private String landlinePhone;

private String mobilePhone;

@NotNull(message = "The username cannot be blank")
@Column(nullable = false, unique = true)
private String username;

@Email(message = "The email address is not valid")
private String email;

@JsonIgnore
private String password;

@Column(nullable = false)
private String timeZone = "Europe/Rome";

@JsonIgnore
private LocalDateTime lastPasswordResetDate;

@Column(nullable = false, columnDefinition = "BOOLEAN default true")
private boolean enabled = true;

@Type(type = "json")
@Column(columnDefinition = "json")
private Roles[] roles = new Roles[] {};

我的枚举角色是:

public enum Roles {
ROLE_ADMIN, ROLE_USER, ROLE_MANAGER, ROLE_TECH;

@JsonCreator
public static Roles create(String value) {
if (value == null) {
throw new IllegalArgumentException();
}
for (Roles v : values()) {
if (value.equals(v.toString())) {
return v;
}
}
throw new IllegalArgumentException();
}
}

我正在 Angular 4 中创建一个客户端。Spring Data REST 很棒,并且公开存储库可以轻松返回我的模型 HATEOAS 兼容:

    {
"_embedded": {
"users": [
{
"name": "Administrator",
"username": "admin",
"roles": [
"Amministratore"
],
"activeWorkSession": "",
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/users/1"
},
"user": {
"href": "http://localhost:8080/api/v1/users/1{?projection}",
"templated": true
}
}
},

就像你所看到的,我还通过rest-messages.properties 翻译我的枚举的值。伟大的!我的 Angular 页面现在需要完整的角色列表(枚举)。我有一个问题:

  • 了解服务器返回角色列表的更好方法
  • 如何返回此列表

我的第一次尝试是创建一个 RepositoryRestController,以便利用 Spring Data REST 提供的功能。

@RepositoryRestController
@RequestMapping(path = "/api/v1")

公共(public)类 UserController {

@Autowired
private EntityLinks entityLinks;

@RequestMapping(method = RequestMethod.GET, path = "/users/roles", produces = "application/json")
public Resource<Roles> findRoles() {
Resource<Roles> resource = new Resource<>(Roles.ROLE_ADMIN);
return resource;
}

不幸的是,由于某种原因,调用此方法会返回 404 错误。我调试了一下,资源创建正确,所以我猜问题出在 JSON 转换的某个地方。

最佳答案

how to return this list?

@RepositoryRestController
@RequestMapping("/roles")
public class RoleController {

@GetMapping
public ResponseEntity<?> getAllRoles() {
List<Resource<Roles>> content = new ArrayList<>();
content.addAll(Arrays.asList(
new Resource<>(Roles.ROLE1 /*, Optional Links */),
new Resource<>(Roles.ROLE2 /*, Optional Links */)));
return ResponseEntity.ok(new Resources<>(content /*, Optional Links */));
}
}

关于spring - 使用 Spring Data REST 公开枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44829475/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com