初学SpringCloud,我的controller层对service层的调用

我的上一篇文章:

初学SpringCloud,service对dao层的调用,简单讲一下我service层的代码

1、本篇博客的简介

          我的这篇文章所在的专栏记录的是我的一个简单的项目过程,像连续剧一样。

2、简单阐述开发的简单的层次规范

         在目前公认的项目规范中,controller层(具体类)调用service层(接口和实现类),service层调用dao层(接口),dao层(接口)配合实体类,mapper.xml,application.yml跟数据库交换数据。

3、简单看一下我的controller层的结构

我的controller层次目前还比较简单,只有一个类,在逻辑上是   支付类

4、解释一下我的PaymentController类中的代码

代码如下:

package com.springcloud.controller;

import com.springcloud.entities.CommonResult;
import com.springcloud.entities.Payment;
import com.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
//下面这个注解的作用我还不是很清楚
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    //对于注册进eureka里面的服务,我们有一个需求,这个需求就是:
    //我们要通过注册中心,来看到这个服务的信息,比如地址啦什么的
    //下面的这个注入的对象就是这个作用
    //我们可以通过服务发现来获取该服务的信息
    @Resource
    private DiscoveryClient discoveryClient;
    //上面的这个包很容易引错,一定要是上面的才行

    @GetMapping("/payment/get/{id}")
    @ResponseBody
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        System.out.println(payment);
        if (payment!=null){
            System.out.println(payment);
            return new CommonResult(200,"查询数据成功,server.port:"+serverPort,payment);
        }else{
            return new CommonResult(500,"查询数据是空",null);

        }
    }

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        //log.info("插入成功"+result);
        if (result>0){
            return new CommonResult(200,"插入数据成功,server.port:"+serverPort,result);

        }else{
            return new CommonResult(500,"插入数据失败",null);

        }
    }

//    @GetMapping(value = "/payment/discovery")
//    public Object discovery(){
//        //得到我们的服务清单列表,盘点一下家底,这个还是需要我认真的理解一下的
//        List<String> services = discoveryClient.getServices();
//        for (String service:services) {
//            //log.info("*******service"+service);
//        }
//        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
//        for (ServiceInstance instance : instances) {
//            //log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
//        }
//
//        return this.discoveryClient;
//    }
}

 上面的是我的代码,其中有一个被注释掉的方法,这个是后期增加功能的时候,会加上去的方法,目前的话还不涉及。

在这里我有一个问题:我在方法内使用log日志的时候,IDEA工具会给我提醒,info()方法也会给我提醒,但就是当启动这一个微服务的时候,控制台会报错。我查找了一些博客,但是目前还没有解决,我感觉是版本的问题(插件版本,JDK版本,Maven版本,IDEA版本)。这个我后期会再查一下,等解决了,再发博客。也欢迎能解决这个问题的小伙伴给我留言。问题截图如下:

@RestController=@Controller+@ResponseBody

controller层肯定都需要@Controller注解

因为dao层次需要调用service层次,所以我就注入了service层的接口

(在这里有个需要注意的,我在这里注入的是接口,可以正常运行。但是我跟我的小伙伴交流的时候,他一直注入的都是那个实现类,也可以正常运行。原先在学校学习的时候,我们使用的是@Autowired注解,老师注入的是接口还是类,我也记不清楚了,哈哈哈哈哈,我不是好学生)

然后就是一个查询的方法,因为是查询,所以使用@GetMapping注解。使用@ResponseBody注解将返回的对象JSON序列化。

然后就调用注入进来的service接口的对象的方法,查询得到一个payment(实体类)

注意:后面的System.out.println()方法,将这个对象输出,输出的是这个对象的地址。

再往下,就进入到一个判断,返回一个CommonResult对象(这也是一个实体类,后面我会记录到的)

5、我的下一篇博客的链接

初学SpringCloud:使用Eureka作为服务注册中心,写服务注册中心的微服务模块

6、补充一下,我的博客中的问题,找到了解决办法,在下面这篇博客中

Lombok插件版本问题,和某一个事物的版本冲突

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你是我的日月星河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值