Spring 从 Angular 请求引导获取本地存储(控制器错误 - 500)
Spring Boot get local storage from Angular Request (Error in Controller - 500)
我不知道如何在 Spring 引导中使用本地存储,我的前端在 Angular 中,我必须将本地存储发送到控制器中,该控制器在 DTO 中转换以从中获取行数据库ext ext,实际上是浏览器return我错误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 启动控制器
@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/"
非常感谢您的帮助。
我修改了这个
sessionUser = (res as unknown as User);
至
sessionUser = (res.body as unknown as User);
现在本地存储只保存对象主体。
我不知道如何在 Spring 引导中使用本地存储,我的前端在 Angular 中,我必须将本地存储发送到控制器中,该控制器在 DTO 中转换以从中获取行数据库ext ext,实际上是浏览器return我错误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 启动控制器
@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/"
非常感谢您的帮助。
我修改了这个
sessionUser = (res as unknown as User);
至
sessionUser = (res.body as unknown as User);
现在本地存储只保存对象主体。