i dont know how use the local storage in Spring boot, my front end is in Angular and i have to send the Local Storage in a controller that convert in DTO to get rows from database ext ext, actually the browser return me error 500
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public ..
The request from Angular
getAttestazioni(): Observable<Posts[]> {
return this.http.post<Posts[]>(this.myAppUrl + this.myApiPostsUrl, this.authService.getLoggedUserFromSessionStorage())
.pipe(
retry(1),
catchError(this.errorHandler)
);
}
Function to get the local storage.
public getLoggedUserFromSessionStorage(): User {
if (localStorage) {
return JSON.parse(localStorage.getItem('currentUser'));
}
return null;
}
The Controller of 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);
}
}
This is my Local Storage, the object is in body:, and maybe for this there isnt the map in the controller.
{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/"
Thanks so much for your help.
I fixed changing this
sessionUser = (res as unknown as User);
to
sessionUser = (res.body as unknown as User);
Now local storage save only the object body .