本文小编为大家详细介绍“Java中Get和Post如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java中Get和Post如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
文件名MyController,内容为:
@RestController
@RequestMapping("/homepage")
publicclass MyController {
@Autowired
MyService myService;
@GetMapping("/learnGet")
public String learnGet(){
return myService.learnGet();
}
}
文件名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;
}
}
在application.properties配置:
# 设置端口号
server.port=8888
配置Get,地址为:http://localhost:8888/homepage/returnTime 。
即可获得当前时间戳。
文件名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);
}
}
文件名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;
}
在application.properties配置:
# 设置端口号
server.port=8888
配置Get,地址为:http://localhost:8888/homepage/returnTime 。
即可获得输出。
需要注意,RestTemplate在postForObject时,用MultiValueMap,不可使用HashMap。
文件名MyController,内容为:
@RestController
@RequestMapping("/homepage")
publicclass MyController {
@Autowired
MyService myService;
@PostMapping("/postSend")
public Map<String, Object> postSend() {
return myService.postSend();
}
}
文件名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;
}
}
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 + "]";
}
}
@Configuration
publicclass Config {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
在application.properties配置:
# 设置端口号
server.port=8889
配置Post,地址为: http://localhost:8889/homepage/postSend
即可获得输出。
读到这里,这篇“Java中Get和Post如何使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。