Java中Get和Post如何使用(get,java,post,开发技术)

时间:2024-05-03 16:45:22 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

本文小编为大家详细介绍“Java中Get和Post如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java中Get和Post如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

1 Get请求数据

1.1 Controller

文件名MyController,内容为:

@RestController@RequestMapping("/homepage")publicclassMyController{@AutowiredMyServicemyService;@GetMapping("/learnGet")publicStringlearnGet(){returnmyService.learnGet();}}

1.2 Service

文件名MyService,内容为:

@Service@EnableSchedulingpublicclassMyService{publicStringlearnGet(){LongtimeLong=System.currentTimeMillis();SimpleDateFormattimeFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置格式StringtimeString=timeFormat.format(timeLong);returntimeString;}}

1.3 Application

在application.properties配置:

#设置端口号server.port=8888

1.4 Postman

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

即可获得当前时间戳。

Java中Get和Post如何使用

2 Post接收数据

2.1 Controller

文件名MyController,内容为:

@RestController@RequestMapping("/homepage")publicclassMyController{@AutowiredMyServicemyService;@PostMapping("/postReceive")publicMap<String,Object>postReceive(@RequestParam("number")intnumber,@RequestParam("name")Stringname){returnmyService.postReceive(number,name);}@PostMapping("/postReceiveByMap")publicMap<String,Object>postReceiveByMap(@RequestParamMap<String,Object>map){System.out.println("map:"+map+"\n");returnmyService.postReceiveByMap(map);}}

2.2 Service

文件名MyService,内容为:

@Service@EnableSchedulingpublicclassMyService{publicMap<String,Object>postReceive(intnumber,Stringname){Map<String,Object>res=newHashMap<>();res.put("number",number);res.put("name",name);returnres;}publicMap<String,Object>postReceiveByMap(Map<String,Object>map){intnumber=map.get("number")==null?0:Integer.parseInt((String)map.get("number"));Stringname=map.get("name")==null?"":(String)map.get("name");Map<String,Object>res=newHashMap<>();res.put("number",number);res.put("name",name);System.out.println("map:"+map+"\n");System.out.println("res:"+res+"\n");returnres;}

2.3 Application

在application.properties配置:

#设置端口号server.port=8888

2.4 Postman

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

即可获得输出。

Java中Get和Post如何使用

3 Post发送数据

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

3.1 Controller

文件名MyController,内容为:

@RestController@RequestMapping("/homepage")publicclassMyController{@AutowiredMyServicemyService;@PostMapping("/postSend")publicMap<String,Object>postSend(){returnmyService.postSend();}}

3.2 Service

文件名MyService,内容为:

@Service@EnableSchedulingpublicclassMyService{@ResourceprivateRestTemplaterestTemplate;StringURL="http://localhost:8888/homepage/postReceiveByMap";publicMap<String,Object>postSend(){Map<String,Object>sendData=newHashMap<>();sendData.put("number",3);sendData.put("name","张三");ResponseEntity<ResponseResult>responseData=restTemplate.postForEntity(URL,sendData,ResponseResult.class);Map<String,Object>returnData=newHashMap<>();returnData.put("StatusCode:",responseData.getStatusCode());returnData.put("Body:",responseData.getBody());returnreturnData;}}

3.3 ResponseResult

publicclassResponseResult{privateintnumber;privateStringname;publicResponseResult(){}publicintgetNumber(){returnnumber;}publicvoidsetNumber(intnumber){this.number=number;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@OverridepublicStringtoString(){return"ResponseResult[number="+number+",name="+name+"]";}}

3.4 Config

@ConfigurationpublicclassConfig{@BeanpublicRestTemplaterestTemplate(RestTemplateBuilderbuilder){returnbuilder.build();}}

3.5 Application

在application.properties配置:

#设置端口号server.port=8889

3.6 Postman

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

即可获得输出。

Java中Get和Post如何使用

读到这里,这篇“Java中Get和Post如何使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

本文:Java中Get和Post如何使用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:python字典怎么获取最大和最小value对应的key下一篇:

8 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18