1. 程式人生 > >通過springboot寫post請求

通過springboot寫post請求

使用工具類

     <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.3.2</version>
        </dependency>

編寫請求類

@Controller
public class UploadController {

    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

    @GetMapping("/posthttp")
    public String postparam(String param,Model model) {
        if (param.equals("")) {
            Person single = new Person("aa", 11);
            model.addAttribute("singlePerson", single);
            model.addAttribute("postcontent", "URL沒有資料");
            return "person";
        } else {
            Person single = new Person("aa", 11);
            model.addAttribute("singlePerson", single);
//        System.out.println("這是提交的引數:"+param);
            HttpRequest httpRequest = HttpRequest.get(param);
            HttpResponse execute = httpRequest.execute();
//        System.out.println("這是結果"+execute.body());
            model.addAttribute("postcontent", execute.toString());
            return "person";
        }

響應

通過springboot寫post請求

通過springboot寫post請求