【问题标题】:Spring Boot get local storage from Angular Request (Error in Controller - 500)Spring Boot 从 Angular 请求获取本地存储(控制器中的错误 - 500)
【发布时间】: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


    【解决方案1】:

    我修复了更改这个

     sessionUser = (res as unknown as User);
    

     sessionUser = (res.body as unknown as User);
    

    现在本地存储只保存对象体。

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 2018-11-24
      • 2021-06-12
      相关资源
      最近更新 更多