首页 > 编程语言 >Java中Get和Post的使用

Java中Get和Post的使用

时间:2022-08-14 21:33:46浏览次数:75  
标签:Map Java name Get number return Post public String

原文链接

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
@EnableScheduling
public 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
@EnableScheduling
public 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
@EnableScheduling
public 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

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

即可获得输出。

 
 

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

代码的路

标签:Map,Java,name,Get,number,return,Post,public,String
From: https://www.cnblogs.com/zbzcDZF/p/16586379.html

相关文章

  • 自学java第10天之接口思想
    图:1.2.3.文字解释: ......
  • JAVA基础环境 一、JDK安装与环境
    JAVA开发者所需的基础环境变量注意*以下内容请在Windows企业版或专业版配置,以免遇到不必要的麻烦一、jdk配置1.jdk下载jdk下载需要去Oracle官网下载,地址是https://www......
  • java学习第七天xml.day18
      反射在java中,反射主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。   获取字节码的方式:  使用反射获取构造器:    内省 ......
  • 自学java第天之obstract抽象类
    父类中,写了抽象方法:什么是抽象方法:publicobstractvoid方法(){},::::::::::::::::::;只有方法名字,没有方法实现那么如果有个类想要继承定义的这个抽象类,那么就要重写父......
  • Java企业级项目智牛股(构建金融交易一体化服务平台,打造全系列课程)
    本人java初级工程师一枚,奈何公司使用的技术比较浅层,业务面比较单一,想着提升一下自己,网上找了很多项目课程都不是很好,后来找到一个这个项目,正好满足我的需求,从整体架构的搭......
  • 自学java第7天
    面向对象011.重写仅仅针对有继承关系的父子类,且重写的是方法,不是属性;2.重写需要有继承关系才能重写,即子类重写父类的方法;3.重写的条件:这个方法是public,且不是static,因为sta......
  • Java基础的简单应用
    packagecom.zhou.partise;importjava.util.Scanner;publicclassTest01{publicstaticvoidmain(String[]args){//写一个计算器,要求实现加减乘除功能,并......
  • Java SE 9 新增特性
    JavaSE9新增特性作者:Grey原文地址:JavaSE9新增特性源码源仓库:Github:java_new_features镜像仓库:GitCode:java_new_featuresJShellJShell是JavaSE9新增的......
  • 6)Java运算符
    Java运算符算术运算符:+,-,*,/,%,++,--;赋值运算符:=关系运算符:>,<,>=,<=,==,!=逻辑运算符:&&,||,!位运算符:&,|,^,~,>>,<<,>>>,条件运算符:?:拓展赋值运算符:+=,-=,*=,/=......
  • 《JavaStudy34》封装
    《封装》......