在Angular调用java web API中传递http.post的参数

时间:2022-02-10 13:39:23

I have a weird situation that may be because I missed something that I didn't realized or know. I am creating a simple login UI using Angular and call the Web API created in java.

我有一个奇怪的情况,可能是因为我错过了一些我没有意识到或知道的东西。我正在使用Angular创建一个简单的登录UI,并调用在java中创建的Web API。

The java web API function is as follows

java web API函数如下

@RequestMapping(value = "/logon", method = RequestMethod.POST, produces = {"application/json"})
@ResponseBody
public String logon(
        @RequestParam(value = "userID", required = true) String userID,
        @RequestParam(value = "password", required = true) String password,
        HttpServletRequest request)

Now if I use the http.post as follows

现在,如果我使用http.post如下

login(username: string, password: string) {
    return this.http.post(this.url+"/security/logon/", 
                          JSON.stringify({ userID: username, password: password }) )

Then I get the following error in the Google Chrome browser:

然后,我在Google Chrome浏览器中收到以下错误:

POST http://localhost:8080/logon/ 400 (Required String parameter 'userID' is not present)

But if I change the code as follows:

但是,如果我更改代码如下:

login(username: string, password: string) {
    var usrpwd = "userID=" + username + "&password=" + password;
    return this.http.post(this.url+"/security/logon?"+usrpwd, usrpwd )

It work perfectly. Am I missing something? Why the second parameter of http.post that should be the parameter passed not seems to be working?

它工作得很好。我错过了什么吗?为什么http.post的第二个参数应该是传递的参数似乎不起作用?

Thanks in advance for any reply or feedback.

提前感谢您的回复或反馈。

2 个解决方案

#1


2  

You are defining your endpoint url with two mandatory parameters, and such parameters must be in the url (check here), so when you make a request to your endpoint, the url must be :

您正在使用两个必需参数定义端点URL,并且此类参数必须位于URL中(请单击此处),因此当您向端点发出请求时,该URL必须为:

http://localhost:8080/logon?userID=yourUserId&password=yourUserPassword

In the first implementation you are not adding the query parameters to the url so the request is made to the url http://localhost:8080/logon/ as it doesn't have the required parameters, your web tier is returning the 400 http code, which implies a bad request (because again, your url doesn't contains the required parameters).

在第一个实现中,您没有将查询参数添加到URL,因此请求URL为http:// localhost:8080 / logon /,因为它没有所需的参数,您的Web层返回400 http代码,这意味着一个错误的请求(因为您的网址不包含所需的参数)。

#2


0  

 constructor(private http:HttpClient){

}

login(usrName,usrPwd){

let body = {userName:usrName, userPwd:usrPwd}; this.http.post("http://localhost:5000/security/login", body) .subscribe( res => console.log(res), error => console.log(error) ) }

#1


2  

You are defining your endpoint url with two mandatory parameters, and such parameters must be in the url (check here), so when you make a request to your endpoint, the url must be :

您正在使用两个必需参数定义端点URL,并且此类参数必须位于URL中(请单击此处),因此当您向端点发出请求时,该URL必须为:

http://localhost:8080/logon?userID=yourUserId&password=yourUserPassword

In the first implementation you are not adding the query parameters to the url so the request is made to the url http://localhost:8080/logon/ as it doesn't have the required parameters, your web tier is returning the 400 http code, which implies a bad request (because again, your url doesn't contains the required parameters).

在第一个实现中,您没有将查询参数添加到URL,因此请求URL为http:// localhost:8080 / logon /,因为它没有所需的参数,您的Web层返回400 http代码,这意味着一个错误的请求(因为您的网址不包含所需的参数)。

#2


0  

 constructor(private http:HttpClient){

}

login(usrName,usrPwd){

let body = {userName:usrName, userPwd:usrPwd}; this.http.post("http://localhost:5000/security/login", body) .subscribe( res => console.log(res), error => console.log(error) ) }