我的数据库中有三个表格,例如user
,authority
和role
。
表权限包含两个字段user_id
和role_id
,它们是外键。
SQL语句是:
SELECT t1.id id, t3.id user_id, t3.username_x username_x, t2.id role_id, t2.desc_x desc_x
FROM `securitydashboard`.`authorities` AS t1
INNER JOIN `securitydashboard`.`role` AS t2
INNER JOIN `securitydashboard`.`user` AS t3
ON (t2.id = t1.role_id
AND t3.id = t1.user_id);
结果将是:
id user_id username_x role_id desc_x
1 1 admin 1 ROLE_ADMIN
2 1 admin 2 ROLE_USER
我正在设置简单的Spring-Hateoas项目。通过“sql视图”获取上述信息并在Traverson上运行会更容易。 我正在努力通过Traverson获取上述信息而没有“sql视图”。 我的java程序如下:
public class UserResource extends ResourceSupport {
public String usernameX;
public String passwordX;
public String firstnameX;
public String lastnameX;
public EmailAddress emailX;
public Boolean enabledB;
public String saltX;
public AuthoritiesesById authority;
static class AuthoritiesesById {
public Roles role;
static class Roles {
String descX;
}
}
}
public class Test2 {
private static final ParameterizedTypeReference<PagedResources<Resource<UserResource>>> TYPE_REFERENCE = new ParameterizedTypeReference<PagedResources<Resource<UserResource>>>() {};
public static void main(String[] args) throws Exception {
String urlstring = "http://localhost:8085/RestDashboard";
URL url = new URL(urlstring);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept", "application/json");
LinkDiscoverer discoverer = new HalLinkDiscoverer();
Link link = discoverer.findLinkWithRel("users", connection.getInputStream());
URI uri = new URI(urlstring);
Traverson traverson = new Traverson(uri, MediaTypes.HAL_JSON);
Map<String, Object> parameters = new HashMap<>();
PagedResources<Resource<UserResource>> resources = traverson.follow(link.getRel()).withTemplateParameters(parameters).toObject(TYPE_REFERENCE);
for (Resource<UserResource> resource : resources) {
System.out.print(resource.getContent().authority.role.descX);
}
}
}
结果返回null。
我做错了什么?
答案 0 :(得分:0)
我不是100%确定是否是问题,但是您从UserResource
延长了ResourceSupport
并且您正在使用TypeReference
键入Resource
,这导致实际上是一样的。
请至少尝试使用ResourceSupport
或者使用Resource
进行扩展,但不要同时使用两个选项。
更新:
尝试使用org.springframework.hateoas.mvc.TypeReferences.PagedResourcesType