gpt4 book ai didi

java http请求所需的字符串参数 'Username'不存在

转载 作者:行者123 更新时间:2023-12-01 18:25:57 25 4
gpt4 key购买 nike

我有一个正在运行的 spring boot 服务器,它接受 http 请求。我已经用curl对其进行了广泛的测试,并且效果非常好。但是,当我尝试在 java 客户端中发送请求并接收响应时,我得到:

java.io.IOException:服务器返回 HTTP 响应代码:URL 为 400:

在服务器端我得到:

[org.springframework.web.bind.MissingServletRequestParameterException:所需的字符串参数“用户名”不存在]

这是我尝试使用的 spring boot 方法:

@PostMapping(path="/CreateAccount") // Map ONLY POST Requests
public @ResponseBody String CreateUser (@RequestParam String Username
, @RequestParam String Password) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
//make sure the username and password is valid input
if (!validateInput(Username) || !validateInput(Password))
{
return "Username and Password cannot be blank";
}
//make sure the username is unique
//check if Username is equal to any other User's username
User u = findByUsername(Username);
//if a user was found in the table
if ( !(u == null) )
{
return "Username already taken\nPlease choose another";
}

//if we are here we are clear to make a new user
User n = new User();
n.setUsername(Username);
n.setPassword(Password);
n.setRole("Player");
userRepository.save(n);

//THIS CAUSES AN ERROR
//create a stat object to be added to the table
Stats s = new Stats();
s.setUsername(Username);
statRepository.save(s);

//create a token for the user
String usrToken = createToken(tokenSize);
//add the username and token to hashmap
userTokens.put(Username, usrToken);

//return this user's token
return usrToken;
}

这是我的客户:

public static void CreateAccount(String username, String password) 
{
try
{
String s = serverURL + "/CreateAccount";
URL url = new URL(s);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setDoOutput(true);

String json = "[{\n\"Username\"=\"Jake\",\"Password\"=\"123\"\n}]";
System.out.println(json);

OutputStream os = connection.getOutputStream();
DataOutputStream out = new DataOutputStream( os );
byte[] input = json.getBytes("utf-8");

out.writeUTF(json);

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = br.readLine();
System.out.println(response);


}
catch (Exception e)
{
System.out.println("#");
System.out.println(e);
}
}

我认为这与我的 json 格式有关,但我已经尝试了一遍又一遍,无论我如何格式化它,我都会得到相同的错误。

最佳答案

如果您想发送 json 对象,请使用以下代码:

@PostMapping(path="/CreateAccount")
public @ResponseBody String CreateUser (@RequestBody User user) {

if (user == null)
{
return "User cannot be blank";
}

User u = findByUsername(user.Username);

// your code...

return usrToken;
}

关于java http请求所需的字符串参数 'Username'不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60235672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com