写点什么

SpringCloud 从入门到精通 04--- 支付模块 02

用户头像
Felix
关注
发布于: 2021 年 01 月 11 日

SpringCloud 从入门到精通 02--- 创建订单模块 01中,我们只是配置了端口信息和数据库信息,并没有写任何的业务逻辑,本节,我们写个最简单的业务逻辑,通过接口实现订单的生成和查询

回到cloud-payment-8001模块中,分别创建程序入口CloudPaymentApplication,通用结果类CommonResult,接口CPaymentService,实现类CPaymentServiceImplmybatis的配置类MybatisConfig ,接口PaymentController,目录分布如下


这里逻辑比较简单,直接贴代码

package com.felix.payment;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class CloudPaymentApplication { public static void main(String[] args) { SpringApplication.run(CloudPaymentApplication.class,args); }}
复制代码


package com.felix.payment.result;
public class CommonResult<T> { private Integer code; private String msg; private String status; private T data;
public CommonResult(Integer code, String msg, String status, T data) { this.code = code; this.msg = msg; this.status = status; this.data = data; } public CommonResult() { }
public static <T> CommonResult<T> success(T data){ return new CommonResult(200,"操作成功","success",data); }
public static <T> CommonResult<T> failed() { return new CommonResult(500, "操作失败", "failed", null); }
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; }
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public T getData() { return data; }
public void setData(T data) { this.data = data; }}
复制代码


package com.felix.payment.service;
import com.felix.payment.result.CommonResult;
import java.util.Map;
public interface CPaymentService { CommonResult createPayment(String serialNo); CommonResult getPayment(Long id);}

复制代码


package com.felix.payment.service.serviceImpl;
import com.felix.mbg.mapper.CPaymentMapper;import com.felix.mbg.model.CPayment;import com.felix.payment.result.CommonResult;import com.felix.payment.service.CPaymentService;import org.springframework.stereotype.Service;
import javax.annotation.Resource;import java.util.HashMap;import java.util.Map;
@Servicepublic class CPaymentServiceImpl implements CPaymentService { @Resource private CPaymentMapper paymentMapper;
public CommonResult createPayment(String serialNo) { CPayment payment = new CPayment(); payment.setSerialNo(serialNo); int result = paymentMapper.insert(payment); if (result <= 0){ return CommonResult.failed(); } return CommonResult.success(payment); }
public CommonResult getPayment(Long id) { CPayment payment = paymentMapper.selectByPrimaryKey(id); if (payment == null){ return CommonResult.failed(); } return CommonResult.success(payment); }}
复制代码


package com.felix.payment.config;
import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Configuration;import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration@EnableTransactionManagement@MapperScan({"com.felix.mbg.mapper"})public class MybatisConfig {}
复制代码


package com.felix.payment.controller;
import com.felix.payment.result.CommonResult;import com.felix.payment.service.CPaymentService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;import java.util.Map;
@Controller@RequestMapping("/payment")public class PaymentController { @Resource private CPaymentService paymentService;
@PostMapping(value = "/create") public CommonResult createPayment(String serialNo){ return paymentService.createPayment(serialNo); }
@GetMapping(value = "/{id}") public CommonResult getPayment(@PathVariable Long id){ return paymentService.getPayment(id); }}
复制代码

到这里,订单的基本逻辑已经可以了,我们使用`postman`测试下结果,先尝试插入一条数据

OK,这里没什么问题,然后获取一下数据

这里在获取数据的时候,发现msg的编码有问题,返回的中文乱码,为了解决这个问题,我们添加个配置文件,修改一下返回数据时的编码方式,在config添加一个类CommonMVCConfiguration实现WebMvcConfigurer,代码如下

package com.felix.payment.config;
import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
@Configurationpublic class CommonMVCConfiguration implements WebMvcConfigurer{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.stream() .filter(converter -> converter instanceof MappingJackson2HttpMessageConverter) .findFirst() .ifPresent(converter -> ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(UTF_8)); }}
复制代码

然后,再次尝试一下获取数据

问题解决,在项目开发中遇到一些异常的问题,建议可以采用搜索引擎google 百度 stackoverflow来进行搜索,对于一些日常问题的解决很有帮助

发布于: 2021 年 01 月 11 日阅读数: 43
用户头像

Felix

关注

还未添加个人签名 2020.12.24 加入

还未添加个人简介

评论 (2 条评论)

发布
用户头像
返回结果最好把泛型给加上吧,不然会出错
2021 年 02 月 24 日 10:31
回复
用户头像
@Controller需要改成@RestController
2021 年 02 月 24 日 09:56
回复
没有更多了
SpringCloud 从入门到精通 04---支付模块 02