我不知道如何在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);
}
}
这是我的本地存储,对象在主体中:也许为此,控制器中没有地图。
{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/"
非常感谢您的帮助。
答案 0 :(得分:0)
我已固定更改
sessionUser = (res as unknown as User);
到
sessionUser = (res.body as unknown as User);
现在本地存储仅保存对象主体。