JAVA 如何做 PUT 请求 REST 客户端
JAVA How to do PUT request REST Client
我正在做一个 REST 服务器和客户端,使用 java 来学习。
一切正常,但来自客户端的 PUT 请求没有正确传递正文中的数据,因为当我在服务器中看到日志时,参数为 NULL
这是客户端PUT请求码:
private static class Mod {
int idUser;
String nameUser;
String passwordUser;
public Mod(int idUser, String nameUser, String passwordUser) {
this.idUser = idUser;
this.nameUser = nameUser;
this.passwordUser = passwordUser;
try {
URL url = new URL("http://localhost:8080/api/users/" + idUser + "/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoInput(true);
connection.setDoOutput(true);
HashMap<String, String> postDataParams = new HashMap<>();
postDataParams.put("nameUser", this.nameUser);
postDataParams.put("passwordUser", this.passwordUser);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write(getPutDataString(postDataParams));
writer.flush();
writer.close();
os.close();
if (connection.getResponseCode() == 201) {
System.out.println("Modified");
connection.disconnect();
} else {
System.out.println("Error");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private String getPutDataString(HashMap<String, String> params) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first) {
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
}
return result.toString();
}
}
这是 POST 请求的代码。它使用与“注入”参数相同的方式并且工作完美:
private static class Create {
String nameUser, passwordUser;
public Create(String nameUser, String passwordUser) {
this.nameUser = nameUser;
this.passwordUser = passwordUser;
try {
URL url = new URL("http://localhost:8080/api/users/");
HttpURLConnection myConnection = (HttpURLConnection) url.openConnection();
myConnection.setRequestMethod("POST");
HashMap<String, String> postDataParams = new HashMap<>();
postDataParams.put("nameUser", this.nameUser);
postDataParams.put("passwordUser", this.passwordUser);
myConnection.setDoInput(true);
myConnection.setDoOutput(true);
OutputStream os = myConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
myConnection.getResponseCode();
if (myConnection.getResponseCode() == 201) {
System.out.println("Created");
myConnection.disconnect();
} else {
System.out.println("Error");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private String getPostDataString(HashMap<String, String> params) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first) {
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
}
return result.toString();
}
}
服务器中的 REST 控制器:
@RestController
@RequestMapping("/api")
public class UserRestController {
@Autowired
private IUserService userService;
@GetMapping("/users")
public List<User> index() {
return userService.findAll();
}
@GetMapping("/users/{id}")
public User show(@PathVariable Long id) {
return this.userService.findById(id);
}
@PostMapping("/users")
@ResponseStatus(HttpStatus.CREATED)
public User create(@ModelAttribute User user) {
this.userService.save(user);
return user;
}
@PutMapping("/users/{id}")
@ResponseStatus(HttpStatus.CREATED)
public User update(@ModelAttribute User user, @PathVariable Long id) {
User currentUser = this.userService.findById(id);
currentUser.setNameUser(user.getNameUser());
currentUser.setPasswordUser(user.getPasswordUser());
this.userService.save(currentUser);
return currentUser;
}
@DeleteMapping("/users/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
this.userService.delete(id);
}
}
您应该使用 x-www-form-urlencoded
,因为您没有发送任何文件。
此外,您需要做更多的工作来手动执行 form-data
请求,因此最好更改服务器以接受 x-www-form-urlencoded
参数。
修改服务器后,将内容类型添加到请求中:
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
并删除这一行:
connection.setDoInput(true)
因为您使用 HttpURLConnection
作为输出。
请注意,您可以使用 Apache HttpClient 轻松发出 PUT 请求。
我正在做一个 REST 服务器和客户端,使用 java 来学习。 一切正常,但来自客户端的 PUT 请求没有正确传递正文中的数据,因为当我在服务器中看到日志时,参数为 NULL
这是客户端PUT请求码:
private static class Mod {
int idUser;
String nameUser;
String passwordUser;
public Mod(int idUser, String nameUser, String passwordUser) {
this.idUser = idUser;
this.nameUser = nameUser;
this.passwordUser = passwordUser;
try {
URL url = new URL("http://localhost:8080/api/users/" + idUser + "/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setDoInput(true);
connection.setDoOutput(true);
HashMap<String, String> postDataParams = new HashMap<>();
postDataParams.put("nameUser", this.nameUser);
postDataParams.put("passwordUser", this.passwordUser);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write(getPutDataString(postDataParams));
writer.flush();
writer.close();
os.close();
if (connection.getResponseCode() == 201) {
System.out.println("Modified");
connection.disconnect();
} else {
System.out.println("Error");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private String getPutDataString(HashMap<String, String> params) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first) {
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
}
return result.toString();
}
}
这是 POST 请求的代码。它使用与“注入”参数相同的方式并且工作完美:
private static class Create {
String nameUser, passwordUser;
public Create(String nameUser, String passwordUser) {
this.nameUser = nameUser;
this.passwordUser = passwordUser;
try {
URL url = new URL("http://localhost:8080/api/users/");
HttpURLConnection myConnection = (HttpURLConnection) url.openConnection();
myConnection.setRequestMethod("POST");
HashMap<String, String> postDataParams = new HashMap<>();
postDataParams.put("nameUser", this.nameUser);
postDataParams.put("passwordUser", this.passwordUser);
myConnection.setDoInput(true);
myConnection.setDoOutput(true);
OutputStream os = myConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
myConnection.getResponseCode();
if (myConnection.getResponseCode() == 201) {
System.out.println("Created");
myConnection.disconnect();
} else {
System.out.println("Error");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private String getPostDataString(HashMap<String, String> params) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first) {
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
}
return result.toString();
}
}
服务器中的 REST 控制器:
@RestController
@RequestMapping("/api")
public class UserRestController {
@Autowired
private IUserService userService;
@GetMapping("/users")
public List<User> index() {
return userService.findAll();
}
@GetMapping("/users/{id}")
public User show(@PathVariable Long id) {
return this.userService.findById(id);
}
@PostMapping("/users")
@ResponseStatus(HttpStatus.CREATED)
public User create(@ModelAttribute User user) {
this.userService.save(user);
return user;
}
@PutMapping("/users/{id}")
@ResponseStatus(HttpStatus.CREATED)
public User update(@ModelAttribute User user, @PathVariable Long id) {
User currentUser = this.userService.findById(id);
currentUser.setNameUser(user.getNameUser());
currentUser.setPasswordUser(user.getPasswordUser());
this.userService.save(currentUser);
return currentUser;
}
@DeleteMapping("/users/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
this.userService.delete(id);
}
}
您应该使用 x-www-form-urlencoded
,因为您没有发送任何文件。
此外,您需要做更多的工作来手动执行 form-data
请求,因此最好更改服务器以接受 x-www-form-urlencoded
参数。
修改服务器后,将内容类型添加到请求中:
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
并删除这一行:
connection.setDoInput(true)
因为您使用 HttpURLConnection
作为输出。
请注意,您可以使用 Apache HttpClient 轻松发出 PUT 请求。