【发布时间】:2020-02-21 13:35:23
【问题描述】:
我不知道如何在 Spring Boot 中使用本地存储,我的前端在 Angular 中,我必须在控制器中发送本地存储,该控制器在 DTO 中转换以从数据库 ext ext 中获取行,实际上浏览器返回错误500
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public ..
来自 Angular 的请求
getAttestazioni(): Observable<Posts[]> {
return this.http.post<Posts[]>(this.myAppUrl + this.myApiPostsUrl, this.authService.getLoggedUserFromSessionStorage())
.pipe(
retry(1),
catchError(this.errorHandler)
);
}
获取本地存储的函数。
public getLoggedUserFromSessionStorage(): User {
if (localStorage) {
return JSON.parse(localStorage.getItem('currentUser'));
}
return null;
}
Spring Boot 的控制器
@RequestMapping(value = "/posts", method = {RequestMethod.GET, RequestMethod.POST})
public ResponseEntity<List<PostDTO>> fetchAll(@RequestBody UserDTO currentUser) {
List<Post> posts;
List<PostDTO> postsDTO = new ArrayList<>();
try {
posts = postService.getAll(currentUser);
if (posts != null) {
postsDTO = postService.fromVOtoDTO(posts, postsDTO);
}
if (postsDTO.isEmpty()) {
System.out.println("No Posts found");
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(attestazioniDTO, HttpStatus.OK);
} catch (AuthenticationException e) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
这是我的本地存储,对象在 body: 中,也许控制器中没有地图。
{headers: {normalizedNames: {}, lazyUpdate: null}, status: 200, statusText: "OK",…}
body: {idUser: "232323", currentGroup: "1213", ip: "192.168.1.2",…}
headers: {normalizedNames: {}, lazyUpdate: null}
ok: true
status: 200
statusText: "OK"
type: 4
url: "http://localhost:8080/api/login/"
非常感谢您的帮助。
【问题讨论】:
标签: angular spring spring-boot local-storage session-storage