发表于: 2018-09-09 23:03:44
1 409
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
测试消费者参数传递:
GET方法:
返回基本类型:
1.提供者添加接口:
@RequestMapping(value="/parameter/{name}")
publicStringparameter(@PathVariable("name") String name){
return"parameter: "+name;
}
2.消费者访问接口:
@RequestMapping(value="/parameter/{name}")
publicStringparameter(@PathVariable("name") String name){
System.out.println(name);
returnrestTemplate.getForEntity("http://hello-service/parameter/name={1}",String.class,name).getBody();
}
3.调用提供者接口:http://127.0.0.1:8081/parameter/z
4.调用消费者接口:http://127.0.0.1:9000/parameter/z
返回对象:
1.提供者、消费者创建实体类文件夹
2.实体类(无参构造函数<必须>):
public classStudent {
privateStringname;
privateLongid;
publicStringgetName() {
returnname;
}
public voidsetName(String name) {
this.name= name;
}
publicLonggetId() {
returnid;
}
public voidsetId(Long id) {
this.id= id;
}
publicStudent(String name,Long id) {
this.name= name;
this.id= id;
}
publicStudent() {
}
@Override
publicStringtoString() {
return"Student{"+"name='"+name+'\''+", id="+id+'}';
}
}
2.提供者编写接口:
@RequestMapping(value= "/object",method= RequestMethod.GET)
publicStudent myObject(@RequestParam("name")String name,@RequestParam("id")longid){
return newStudent(name,id);
}
3.消费者接口:
@GetMapping(value= "/object")
publicStudent myObject(){
returnrestTemplate.getForEntity("http://hello-service/object?name={1}&id={2}",Student.class,"小明",1L).getBody();
}
4.postman测试提供者接口:
5.postman测试消费者接口:
getForEntity与getForObject
提供者:
@GetMapping(value= "/getTest")
publicStudent getTest(@RequestParam("name")String name,@RequestParam("id")longid){
return newStudent(name,id);
}
消费者:
@GetMapping(value= "/getTest")
publicStudent getTest(){
returnrestTemplate.getForObject("http://hello-service/getTest?name={1}&id={2}",Student.class,"小米",2L);
}
POST方法:
提供者:
@PostMapping(value= "/postTest")
publicStudent postTest(@RequestBodyStudent student){
returnstudent;
}
消费者:
@PostMapping(value= "/postTest")
publicStudent postTest(){
Student student=newStudent("小王",3L);
returnrestTemplate.postForObject("http://hello-service/postTest",student,Student.class);
}
postman测试提供者:
postman测试消费者:
put请求:(无返回值)
提供者:
@PutMapping(value= "/putTest")
public voidputTest(@RequestBodyStudent student){
System.out.println(student);
}
消费者:
@PutMapping(value= "putTest")
public voidputTest(){
Student student=newStudent("南航",5L);
restTemplate.put("http://hello-service/putTest",student);
}
postman测试:
提供者输出:
Delete
提供者:
@RequestMapping(value="/delete")
public voiddelete(@RequestParam("id")longid){
System.out.println(id);
}
消费者:
@RequestMapping("/delete")
public voiddelete() {
restTemplate.delete("http://hello-service/delete?id={1}",100L);
}
postman测试:
明天计划的事情:(一定要写非常细致的内容)
学习其它组件;
遇到的问题:(遇到什么困难,怎么解决的)
效率太低;
收获:(通过今天的学习,学到了什么知识)
如上;
评论