1 Get請(qǐng)求數(shù)據(jù)
項(xiàng)目地址:https://github.com/Snowstorm0/learn-get-post
1.1 Controller
文件名MyController,內(nèi)容為:
@RestController
@RequestMapping("/homepage")
public class MyController {
@Autowired
MyService myService;
@GetMapping("/learnGet")
public String learnGet(){
return myService.learnGet();
}
}
1.2 Service
文件名MyService,內(nèi)容為:
@Service
@EnableScheduling
public class 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è)置端口號(hào)
server.port=8888
1.4 Postman
配置Get,地址為: http://localhost:8888/homepage/returnTime 。
即可獲得當(dāng)前時(shí)間戳。
2 Post接收數(shù)據(jù)
項(xiàng)目地址:https://github.com/Snowstorm0/learn-get-post
2.1 Controller
文件名MyController,內(nèi)容為:
@RestController
@RequestMapping("/homepage")
public class MyController {
@Autowired
MyService myService;
@PostMapping("/postReceive")
public Map postReceive(@RequestParam("number") int number, @RequestParam("name") String name) {
return myService.postReceive(number, name);
}
@PostMapping("/postReceiveByMap")
public Map postReceiveByMap(@RequestParam Map map) {
System.out.println("map:" + map + "\\n");
return myService.postReceiveByMap(map);
}
}
2.2 Service
文件名MyService,內(nèi)容為:
@Service
@EnableScheduling
public class MyService {
public Map postReceive(int number, String name) {
Map res = new HashMap<>();
res.put("number", number);
res.put("name", name);
return res;
}
public Map postReceiveByMap(Map 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 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è)置端口號(hào)
server.port=8888
2.4 Postman
配置Get,地址為: http://localhost:8888/homepage/returnTime 。
即可獲得輸出。
3 Post發(fā)送數(shù)據(jù)
項(xiàng)目地址:https://github.com/Snowstorm0/learn-post-send
需要注意,RestTemplate在postForObject時(shí),用MultiValueMap,不可使用HashMap。
3.1 Controller
文件名MyController,內(nèi)容為:
@RestController
@RequestMapping("/homepage")
public class MyController {
@Autowired
MyService myService;
@PostMapping("/postSend")
public Map postSend() {
return myService.postSend();
}
}
3.2 Service
文件名MyService,內(nèi)容為:
@Service
@EnableScheduling
public class MyService {
@Resource
private RestTemplate restTemplate;
String URL = "http://localhost:8888/homepage/postReceiveByMap";
public Map postSend() {
Map sendData = new HashMap<>();
sendData.put("number", 3);
sendData.put("name", "張三");
ResponseEntity responseData = restTemplate.postForEntity(URL, sendData, ResponseResult.class);
Map 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配置:
# 設(shè)置端口號(hào)
server.port=8889
3.6 Postman
配置Post,地址為: http://localhost:8889/homepage/postSend
即可獲得輸出。
審核編輯:湯梓紅
-
JAVA
+關(guān)注
關(guān)注
19文章
2952瀏覽量
104489 -
Controller
+關(guān)注
關(guān)注
0文章
397瀏覽量
57066 -
GitHub
+關(guān)注
關(guān)注
3文章
465瀏覽量
16359
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論