一文带你搞懂Java中Get和Post的使用

 更新时间:2022年11月15日 16:50:17   作者:代码的路  
这篇文章主要为大家详细介绍了Java中Get和Post用法的相关资料,文中的示例代码讲解详细,对我们学习Java有一定的帮助,需要的可以参考一下

1 Get请求数据

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

1.1 Controller

文件名MyController,内容为:

@RestController
@RequestMapping("/homepage")
publicclass MyController {
    @Autowired
    MyService myService;

    @GetMapping("/learnGet")
    public String learnGet(){
        return myService.learnGet();
    }
}

1.2 Service

文件名MyService,内容为:

@Service
@EnableScheduling
publicclass 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")
publicclass 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
@EnableScheduling
publicclass 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")
publicclass MyController {

    @Autowired
    MyService myService;

    @PostMapping("/postSend")
    public Map<String, Object> postSend() {
        return myService.postSend();
    }
}

3.2 Service

文件名MyService,内容为:

@Service
@EnableScheduling
publicclass 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

publicclass ResponseResult {

    privateint 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

@Configuration
publicclass 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

即可获得输出。

以上就是一文带你搞懂Java中Get和Post的使用的详细内容,更多关于Java Get Post的资料请关注脚本之家其它相关文章!

相关文章

  • 消息中间件详解以及比较选择

    消息中间件详解以及比较选择

    这篇文章主要介绍了消息中间件详解以及比较选择,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java中ArrayList类的用法与源码完全解析

    Java中ArrayList类的用法与源码完全解析

    这篇文章主要介绍了Java中ArrayList类的用法与源码完全解析,ArrayList类通过List接口实现,是Java中引申出的一种数据结构,需要的朋友可以参考下
    2016-05-05
  • springboot高并发下提高吞吐量的实现

    springboot高并发下提高吞吐量的实现

    这篇文章主要介绍了springboot高并发下提高吞吐量的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • java发送kafka事务消息的实现方法

    java发送kafka事务消息的实现方法

    本文主要介绍了java发送kafka事务消息的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Springboot如何去掉URL后面的jsessionid

    Springboot如何去掉URL后面的jsessionid

    这篇文章主要介绍了Springboot如何去掉URL后面的jsessionid,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot整合RabbitMQ实现流量消峰

    SpringBoot整合RabbitMQ实现流量消峰

    RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用,消息中间件在互联网公司的使用中越来越多,本文给大家介绍了SpringBoot整合RabbitMQ实现流量消峰,需要的朋友可以参考下
    2024-12-12
  • Mybatis-Plus BaseMapper的用法详解

    Mybatis-Plus BaseMapper的用法详解

    这篇文章主要介绍了Mybatis-Plus BaseMapper的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Java8时间日期库中的常用使用示例

    Java8时间日期库中的常用使用示例

    这篇文章主要介绍了Java8时间日期库中的20个常用使用示例,帮助大家更好学习Java8是如何处理时间及日期的方法,感兴趣的朋友可以参考一下
    2016-02-02
  • Java操作文件路径正反斜杠问题解决

    Java操作文件路径正反斜杠问题解决

    最近在实现文件上传时,windows与linux系统出现的问题,两个系统中操作文件使用"\","/"导致IOException,本文主要介绍了Java操作文件路径正反斜杠问题解决,感兴趣的可以了解一下啊
    2024-01-01
  • java基础学习JVM中GC的算法

    java基础学习JVM中GC的算法

    这篇文章主要介绍了java基础学习JVM中GC的算法,通过图文加深对GC算法思路的理解。
    2017-11-11

最新评论