【问题标题】:Send data post in angular以角度发送数据帖子
【发布时间】:2019-05-24 17:11:08
【问题描述】:

什么问题,当我尝试从前端发送 dato 到后端时,我得到错误 400,我不明白是什么问题。

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http';


@Injectable({
    providedIn: 'root'
})
export class ListUserService {

    constructor(public http: HttpClient) {
        this.http.get(`http://localhost:8080/user/getUsers/`)
            .subscribe(response => {
                console.log(response);
            });
    }

addFriend(user) {
        const config = new HttpHeaders().set('Content-Type', 'application/json').set('Accept', 'application/json')
        const url = 'http://localhost:8080/meet/friend?idOwner=?&idUser=?';
        console.log('SS', user.id);
        const body = JSON.stringify({"idOwner": 89.9, "idUser": 89.9});

        return this.http.post(url, body, {headers: config}).subscribe(response => {
            console.log('jkljl', response);
        });
    }
    }

这就是spring-java中的enpoint,endpoint需要两个参数,一个id-Owner和id-User

   @RequestMapping(
            value = "/friend/{idUser}/owner/{idOwner}",
            method = RequestMethod.POST
    )
    public ResponseEntity<Meet> friend(  @PathVariable Long idUser,@PathVariable Long idOwner) {
        log.info("PUSEN " +idOwner+"   "+idUser);
        return new ResponseEntity<Meet>(meetService.createMeetWithFriend(idOwner, idUser), HttpStatus.OK);
    }

我得到这个错误:

019-05-24 14:30:18.518  WARN 9840 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/meet/friend] in DispatcherServlet with name
'dispatcherServlet'

【问题讨论】:

  • @RequestParam 是查询参数的still不是请求正文。
  • @jonrsharpe 我应该改变它吗?我不知道该放什么
  • 你应该改变一侧或另一侧,因为它们目前不匹配,但我们不能告诉你哪一个。当您决定 API 实际应该是什么时,Spring Boot docs 可以帮助您实现它。我也评价baeldung.com/spring-boot
  • @jonrsharpe 我在 spring java 中用端点编辑了我的问题
  • 所以你希望数据在路径中?因为你也没有这样做。最后一次:客户端请求必须与服务器期望的一致。除非是这样,否则期望它起作用是不合理的。选择一个 API,然后双方写入该 API

标签: angular


【解决方案1】:

当您在 Spring Boot 应用程序中发布帖子时,您需要在 Spring Rest Controller 中进行一些调整。

@RequestParam 用于从 URL 中获取查询参数,例如:meet/friend? idOwner=1&idUser=2 这不是你的情况。您需要作为 RequestBody 接收。

我建议你创建一个简单的 Java Pojo 类,它只包含你打算接收的字段,例如:

public class Friend {
    public Long id1;
    public Long id2;

   // Getters and Setters
}

然后,您在 java 控制器中更改您的方法以接收这个新类,例如:

public ResponseEntity<Meet> friend(@RequestBody Friend friend) {
    // Log what you need here
    return new ResponseEntity<Meet>(meetService.createMeetWithFriend(friend.getId1(), friend.getId2()), HttpStatus.OK);
}

注意pojo类的属性名,必须和你在angular中Stringfy的json属性名一致。

【讨论】:

  • 我不想创建 pojo,拜托
  • 我得到了这个错误 019-05-24 14:30:18.518 WARN 9840 --- [nio-8080-exec-2] os.web.servlet.PageNotFound : 没有为 HTTP 请求找到映射DispatcherServlet 中名称为“dispatcherServlet”的 URI [/meet/friend]
  • @noseJ 您可以在更改后通过 URL 接收,但现在您也必须更改您的角度。您的 Angular 代码正在向 /meet/friend 发布一个正文,并且您的 Spring 控制器期望接收 /meet/friend/1/owner/2 之类的参数。如您所见,URL 不匹配。如果您想以这种方式保留 Spring 控制器,则必须更改 Angular 代码以发送与 Spring 控制器相同的 id。
  • 我编辑了我的问题,如何更改网址?我的网址有问题
猜你喜欢
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
相关资源
最近更新 更多