写点什么

Java 中 Get 和 Post 的使用

作者:代码的路
  • 2023-01-12
    江苏
  • 本文字数:2251 字

    阅读完需:约 7 分钟

原文链接

1 Get 请求数据

项目地址:https://github.com/Snowstorm0/learn-get-post

1.1 Controller

文件名 MyController,内容为:


@RestController@RequestMapping("/homepage")public class MyController {    @Autowired    MyService myService;
@GetMapping("/learnGet") public String learnGet(){ return myService.learnGet(); }}
复制代码

1.2 Service

文件名 MyService,内容为:


@Service@EnableSchedulingpublic class MyService {    public String learnGet(){        Long timeLong = System.currentTimeMillis();        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式        String timeString = timeFormat.format(timeLong);        return timeString;    }}
复制代码

1.3 Application

在 application.properties 配置:


# 设置端口号server.port=8888
复制代码

1.4 Postman

配置 Get,地址为: http://localhost:8888/homepage/returnTime 。


即可获得当前时间戳。


2 Post 接收数据

项目地址:https://github.com/Snowstorm0/learn-get-post

2.1 Controller

文件名 MyController,内容为:


@RestController@RequestMapping("/homepage")public class MyController {    @Autowired    MyService myService;    @PostMapping("/postReceive")    public Map<String, Object> postReceive(@RequestParam("number") int number, @RequestParam("name") String name) {        return myService.postReceive(number, name);    }    @PostMapping("/postReceiveByMap")    public Map<String, Object> postReceiveByMap(@RequestParam Map<String, Object> map) {        System.out.println("map:" + map + "\n");        return myService.postReceiveByMap(map);    }}
复制代码

2.2 Service

文件名 MyService,内容为:


@Service@EnableSchedulingpublic class MyService {    public Map<String, Object> postReceive(int number, String name){        Map<String, Object> res = new HashMap<>();        res.put("number", number);        res.put("name", name);        return res;    }    public Map<String, Object> postReceiveByMap(Map<String, Object> map){        int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number"));        String name = map.get("name") == null ? "" : (String)map.get("name");        Map<String, Object> res = new HashMap<>();        res.put("number", number);        res.put("name", name);        System.out.println("map:" + map + "\n");        System.out.println("res:" + res + "\n");        return res;    }
复制代码

2.3 Application

在 application.properties 配置:


# 设置端口号server.port=8888
复制代码

2.4 Postman

配置 Get,地址为: http://localhost:8888/homepage/returnTime 。


即可获得输出。


3 Post 发送数据

项目地址:https://github.com/Snowstorm0/learn-post-send


需要注意,RestTemplate 在 postForObject 时,用 MultiValueMap,不可使用 HashMap。

3.1 Controller

文件名 MyController,内容为:


@RestController@RequestMapping("/homepage")public class MyController {
@Autowired MyService myService;
@PostMapping("/postSend") public Map<String, Object> postSend() { return myService.postSend(); }}
复制代码

3.2 Service

文件名 MyService,内容为:


@Service@EnableSchedulingpublic class MyService {    @Resource    private RestTemplate restTemplate;    String URL = "http://localhost:8888/homepage/postReceiveByMap";
public Map<String, Object> postSend(){ Map<String, Object> sendData = new HashMap<>(); sendData.put("number", 3); sendData.put("name", "张三"); ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(URL, sendData, ResponseResult.class); Map<String, Object> returnData = new HashMap<>(); returnData.put("StatusCode:", responseData.getStatusCode()); returnData.put("Body:", responseData.getBody()); return returnData; }}
复制代码

3.3 ResponseResult

public class ResponseResult {
private int number; private String name;
public ResponseResult(){ }
public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; }
@Override public String toString() { return "ResponseResult [number=" + number + ",name=" + name + "]"; }}
复制代码

3.4 Config

@Configurationpublic class Config {    @Bean    public RestTemplate restTemplate(RestTemplateBuilder builder){        return builder.build();    }}
复制代码

3.5 Application

在 application.properties 配置:


# 设置端口号server.port=8889
复制代码

3.6 Postman

配置 Post,地址为: http://localhost:8889/homepage/postSend


即可获得输出。



学习更多编程知识,请关注我的公众号:


代码的路



发布于: 刚刚阅读数: 4
用户头像

代码的路

关注

公众号:代码的路 2023-01-10 加入

Java、Python、C++、图像处理、深度学习相关知识分享

评论

发布
暂无评论
Java中Get和Post的使用_Java_代码的路_InfoQ写作社区