1.创建实体类模块

2.导入lombok
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
3.添加实体类
package com.cx;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
@Data
public class Order {
private Long id;
private BigDecimal totalAmount;
private Long userId;
private String nickName;
private String address;
private List<Product> productList;
}
package com.cx;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class Product {
private Long id;
private BigDecimal price;
private String productName;
private int num;
}
4.在services的pom文件中导入model
<dependency>
<groupId>com.cx</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5.基本流程


6.product模块代码
package com.cx.product.controller;
import com.cx.Product;
import com.cx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@Autowired
ProductService productService;
@GetMapping(value = "/productId/{id}")
public Product getProductById(@PathVariable("id") Long productId) {
return productService.getProductById(productId);
}
}
package com.cx.product.service;
import com.cx.Product;
public interface ProductService {
Product getProductById(Long productId);
}
package com.cx.product.service.impl;
import com.cx.Product;
import com.cx.product.service.ProductService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class ProductServiceImpl implements ProductService {
@Override
public Product getProductById(Long productId) {
Product product = new Product();
product.setId(productId);
product.setPrice(new BigDecimal("99"));
product.setProductName("苹果-" + productId);
product.setNum(11);
return product;
}
}
7.order模块代码
package com.cx.order.controller;
import com.cx.Order;
import com.cx.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@GetMapping(value = "/create")
public Order createOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId) {
return orderService.createOrder(userId, productId);
}
}
package com.cx.order.service;
import com.cx.Order;
public interface OrderService {
Order createOrder(Long userId, Long productId);
}
package com.cx.order.service.impl;
import com.cx.Order;
import com.cx.order.service.OrderService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
@Service
public class OrderServiceImpl implements OrderService {
@Override
public Order createOrder(Long userId, Long productId) {
Order order = new Order();
order.setId(1L);
order.setTotalAmount(new BigDecimal("0"));
order.setUserId(userId);
order.setNickName("张三");
order.setAddress("火星");
order.setProductList(null);
return order;
}
}
《编写微服务api》 是转载文章,点击查看原文。