最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年11月15日 16:50:17   作者:代碼的路  
這篇文章主要為大家詳細(xì)介紹了Java中Get和Post用法的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下

1 Get請求數(shù)據(jù)

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

1.1 Controller

文件名MyController,內(nèi)容為:

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

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

1.2 Service

文件名MyService,內(nèi)容為:

@Service
@EnableScheduling
publicclass MyService {
    public String learnGet(){
        Long timeLong = System.currentTimeMillis();
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //設(shè)置格式
        String timeString = timeFormat.format(timeLong);
        return timeString;
    }
}

1.3 Application

在application.properties配置:

# 設(shè)置端口號
server.port=8888

1.4 Postman

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

即可獲得當(dāng)前時間戳。

2 Post接收數(shù)據(jù)

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

2.1 Controller

文件名MyController,內(nèi)容為:

@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,內(nèi)容為:

@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配置:

# 設(shè)置端口號
server.port=8888

2.4 Postman

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

即可獲得輸出。

3 Post發(fā)送數(shù)據(jù)

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

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

3.1 Controller

文件名MyController,內(nèi)容為:

@RestController
@RequestMapping("/homepage")
publicclass MyController {

    @Autowired
    MyService myService;

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

3.2 Service

文件名MyService,內(nèi)容為:

@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配置:

# 設(shè)置端口號
server.port=8889

3.6 Postman

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

即可獲得輸出。

以上就是一文帶你搞懂Java中Get和Post的使用的詳細(xì)內(nèi)容,更多關(guān)于Java Get Post的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 消息中間件詳解以及比較選擇

    消息中間件詳解以及比較選擇

    這篇文章主要介紹了消息中間件詳解以及比較選擇,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java中ArrayList類的用法與源碼完全解析

    Java中ArrayList類的用法與源碼完全解析

    這篇文章主要介紹了Java中ArrayList類的用法與源碼完全解析,ArrayList類通過List接口實現(xiàn),是Java中引申出的一種數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下
    2016-05-05
  • springboot高并發(fā)下提高吞吐量的實現(xiàn)

    springboot高并發(fā)下提高吞吐量的實現(xiàn)

    這篇文章主要介紹了springboot高并發(fā)下提高吞吐量的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • java發(fā)送kafka事務(wù)消息的實現(xiàn)方法

    java發(fā)送kafka事務(wù)消息的實現(xiàn)方法

    本文主要介紹了java發(fā)送kafka事務(wù)消息的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Springboot如何去掉URL后面的jsessionid

    Springboot如何去掉URL后面的jsessionid

    這篇文章主要介紹了Springboot如何去掉URL后面的jsessionid,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot整合RabbitMQ實現(xiàn)流量消峰

    SpringBoot整合RabbitMQ實現(xiàn)流量消峰

    RabbitMQ 即一個消息隊列,主要是用來實現(xiàn)應(yīng)用程序的異步和解耦,同時也能起到消息緩沖,消息分發(fā)的作用,消息中間件在互聯(lián)網(wǎng)公司的使用中越來越多,本文給大家介紹了SpringBoot整合RabbitMQ實現(xiàn)流量消峰,需要的朋友可以參考下
    2024-12-12
  • Mybatis-Plus BaseMapper的用法詳解

    Mybatis-Plus BaseMapper的用法詳解

    這篇文章主要介紹了Mybatis-Plus BaseMapper的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java8時間日期庫中的常用使用示例

    Java8時間日期庫中的常用使用示例

    這篇文章主要介紹了Java8時間日期庫中的20個常用使用示例,幫助大家更好學(xué)習(xí)Java8是如何處理時間及日期的方法,感興趣的朋友可以參考一下
    2016-02-02
  • Java操作文件路徑正反斜杠問題解決

    Java操作文件路徑正反斜杠問題解決

    最近在實現(xiàn)文件上傳時,windows與linux系統(tǒng)出現(xiàn)的問題,兩個系統(tǒng)中操作文件使用"\","/"導(dǎo)致IOException,本文主要介紹了Java操作文件路徑正反斜杠問題解決,感興趣的可以了解一下啊
    2024-01-01
  • java基礎(chǔ)學(xué)習(xí)JVM中GC的算法

    java基礎(chǔ)學(xué)習(xí)JVM中GC的算法

    這篇文章主要介紹了java基礎(chǔ)學(xué)習(xí)JVM中GC的算法,通過圖文加深對GC算法思路的理解。
    2017-11-11

最新評論

连平县| 彩票| 固始县| 开原市| 察哈| 凉山| 高平市| 宜都市| 厦门市| 历史| 古浪县| 双峰县| 清河县| 宝坻区| 永靖县| 东方市| 桦南县| 麻江县| 黔西| 芷江| 聂拉木县| 南投县| 德州市| 伊金霍洛旗| 防城港市| 韶山市| 武川县| 嘉祥县| 阿合奇县| 盱眙县| 永新县| 美姑县| 石首市| 固始县| 乐陵市| 庆阳市| 汕头市| 闽清县| 罗江县| 博客| 平度市|