测试添加修改时不要忘记在提供端使用@ReqestBody注解,否则插入的值都是空
1 .新建工程cloud-consumer-order80 ,
2 .pom.xml依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies>
2 .yml文件
server:
port: 80
3 .controller类
@RestController @Slf4j public class OrderController { public static final String PAYMENT_URL="http://localhost:8001"; @Resource private RestTemplate restTemplate; @PostMapping("/consumer/addPayment") public CommonResult addPayment(Payment payment){ return restTemplate.postForObject(PAYMENT_URL+"/admin/addPayment",payment,CommonResult.class); } @GetMapping("/consumer/findPaymentById") public CommonResult findPaymentById(@PathVariable("id")Long id){ log.info("消费端查找数据"); return restTemplate.getForObject(PAYMENT_URL+"/admin/findPaymentById"+id,CommonResult.class); } }
4 RestTemplate配置类
@Configuration public class ApplicationConfig { @Bean public RestTemplate getTemplate(){ return new RestTemplate(); } }
5 .主启动类
@SpringBootApplication public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); } }
6 .使用Postman测试