繁体   English   中英

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 Controller:

@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作为 output。

请注意,您可以使用Apache HttpClient轻松发出 PUT 请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM