从 HttpServletResponse 返回 HAL json
Returning HAL json from HttpServletResponse
我正在尝试 return 我的 "successfullAuthentication" 方法中的响应主体 "UsernamePasswordAuthenticationFilter" 使用 HATEOAS,但是 return 链接格式如下:
"links": [
{
"rel": "self",
"href": "http://localhost:8080/api/users/5c55ee26911e9f04acb77c91",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null
},
我希望它是 return HAL json 格式,所以它看起来像这样:
"_links": {
"self": {
"href": "http://localhost:8080/api/users/5c55ee26911e9f04acb77c91"
},
我的方法中有这个(响应是 HttpServletResponse):
User user = userService.findById(authResult.getName());
String json = Jackson.toJsonString(userResourceAssembler.toResource(user));
response.setContentType("application/hal+json");
response.setCharacterEncoding("UTF-8");
response.addHeader(jwtConfig.getHeader(), jwtConfig.getPrefix() + token);
response.getWriter().write(json);
我的 WebConfig 中也有这个:@EnableHypermediaSupport(type = { EnableHypermediaSupport.HypermediaType.HAL })
有人知道为什么会这样吗?
尝试扩展所有模型 classes- 添加 HATEOAS 链接 –
和org.springframework.hateoas.ResourceSupport class
假设您在控制器中有相应的 URI class
Link link = ControllerLinkBuilder
.linkTo(UserController.class)
.slash(user.getXXX())
.withSelfRel();
//for single resource
user.add(link);
Link userLink = ControllerLinkBuilder
.linkTo(ControllerLinkBuilder
.methodOn(UserController.class).getAllUsers())
.withSelfRel();
//For collections
userList.add(userLink);
我在这个 github 问题中找到了答案:https://github.com/spring-projects/spring-hateoas/issues/270#issuecomment-145606558
基本上:
private String convertToHalString(ResourceSupport resource) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jackson2HalModule());
mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(
new EvoInflectorRelProvider(), null, null));
String resourceString = null;
try {
resourceString = mapper.writeValueAsString(resource);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return resourceString;
}
我正在尝试 return 我的 "successfullAuthentication" 方法中的响应主体 "UsernamePasswordAuthenticationFilter" 使用 HATEOAS,但是 return 链接格式如下:
"links": [
{
"rel": "self",
"href": "http://localhost:8080/api/users/5c55ee26911e9f04acb77c91",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null
},
我希望它是 return HAL json 格式,所以它看起来像这样:
"_links": {
"self": {
"href": "http://localhost:8080/api/users/5c55ee26911e9f04acb77c91"
},
我的方法中有这个(响应是 HttpServletResponse):
User user = userService.findById(authResult.getName());
String json = Jackson.toJsonString(userResourceAssembler.toResource(user));
response.setContentType("application/hal+json");
response.setCharacterEncoding("UTF-8");
response.addHeader(jwtConfig.getHeader(), jwtConfig.getPrefix() + token);
response.getWriter().write(json);
我的 WebConfig 中也有这个:@EnableHypermediaSupport(type = { EnableHypermediaSupport.HypermediaType.HAL })
有人知道为什么会这样吗?
尝试扩展所有模型 classes- 添加 HATEOAS 链接 –
和org.springframework.hateoas.ResourceSupport class
假设您在控制器中有相应的 URI class
Link link = ControllerLinkBuilder
.linkTo(UserController.class)
.slash(user.getXXX())
.withSelfRel();
//for single resource
user.add(link);
Link userLink = ControllerLinkBuilder
.linkTo(ControllerLinkBuilder
.methodOn(UserController.class).getAllUsers())
.withSelfRel();
//For collections
userList.add(userLink);
我在这个 github 问题中找到了答案:https://github.com/spring-projects/spring-hateoas/issues/270#issuecomment-145606558
基本上:
private String convertToHalString(ResourceSupport resource) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jackson2HalModule());
mapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(
new EvoInflectorRelProvider(), null, null));
String resourceString = null;
try {
resourceString = mapper.writeValueAsString(resource);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return resourceString;
}