编写微服务api

1.创建实体类模块

2.导入lombok

XML 复制代码
<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

3.添加实体类

java 复制代码
package com.cx;

import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Jiang
 * @date 2025/11/7
 */
@Data
public class Order {
    private Long id;
    private BigDecimal totalAmount;
    private Long userId;
    private String nickName;
    private String address;
    private List<Product> productList;
}
java 复制代码
package com.cx;

import lombok.Data;

import java.math.BigDecimal;

/**
 * @author Jiang
 * @date 2025/11/7
 */
@Data
public class Product {
    private Long id;
    private BigDecimal price;
    private String productName;
    private int num;
}

4.在services的pom文件中导入model

XML 复制代码
 <dependency>
    <groupId>com.cx</groupId>
    <artifactId>model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

5.基本流程

6.product模块代码

java 复制代码
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;

/**
 * @author Jiang
 * @date 2025/11/7
 */
@RestController
public class ProductController {
    @Autowired
    ProductService productService;

    @GetMapping(value = "/productId/{id}")
    public Product getProductById(@PathVariable("id") Long productId) {
        return productService.getProductById(productId);
    }
}
java 复制代码
package com.cx.product.service;

import com.cx.Product;

/**
 * @author Jiang
 * @date 2025/11/7
 */
public interface ProductService {
    Product getProductById(Long productId);
}
java 复制代码
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;

/**
 * @author Jiang
 * @date 2025/11/7
 */
@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模块代码

java 复制代码
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;

/**
 * @author Jiang
 * @date 2025/11/7
 */
@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);
    }
}
java 复制代码
package com.cx.order.service;

import com.cx.Order;

/**
 * @author Jiang
 * @date 2025/11/7
 */
public interface OrderService {
    Order createOrder(Long userId, Long productId);
}
java 复制代码
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;

/**
 * @author Jiang
 * @date 2025/11/7
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Override
    public Order createOrder(Long userId, Long productId) {
        Order order = new Order();
        order.setId(1L);
        //TODO 总金额
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(userId);
        order.setNickName("张三");
        order.setAddress("火星");
        //TODO 远程查询商品列表
        order.setProductList(null);
        return order;
    }
}
相关推荐
涔溪11 分钟前
如何解决微前端架构中主应用和微应用的通信问题?
前端·架构
Xの哲學4 小时前
Linux 指针工作原理深入解析
linux·服务器·网络·架构·边缘计算
踏浪无痕4 小时前
手写一个Nacos配置中心:搞懂长轮询推送机制(附完整源码)
后端·面试·架构
努力进修4 小时前
跨设备文件共享零烦恼!PicoShare+cpolar让跨设备传输更简单
云原生·eureka·cpolar
Mintopia5 小时前
无界通信与主题切换:当主系统邂逅子系统的浪漫史
架构·前端框架·前端工程化
r***93485 小时前
CentOS7安装Mysql5.7(ARM64架构)
adb·架构
gAlAxy...6 小时前
SpringMVC 框架从入门到实践:架构解析与案例实现
架构
ALex_zry12 小时前
Docker Compose运维技术实战分享:从安装到架构解析
运维·docker·架构
不爱吃糖的程序媛16 小时前
华为 CANN:昇腾 AI 的异构计算架构核心与开源生态解析
人工智能·华为·架构