温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

分布式商城项目–编写UserService测试接口

发布时间:2020-06-28 08:03:25 来源:网络 阅读:243 作者:yc王志威 栏目:编程语言

接下来我们来编写一个测试接口,测试整个项目能否成功运行
首先我们在数据库的“user”表中增加一条记录,作为测试使用;
分布式商城项目–编写UserService测试接口
这里我们做这样一个测试,编写一个方法去通过userid,去查询用户的信息,并且将信息返回到页面
逆向工程中已经为我们提供了selectUserByPrimarykey()的方法,所以只要调用此方法就可以。
现在我们在ycshop-manager-interfaces模块创建包cn.yuechenc.ycshop.manager.interfaces;
在此包里面创建接口:

package cn.yuechenc.ycshop.manager.interfaces;

import cn.yuechenc.pojo.User;

public interface UserService {
    public User selectUserByPrimarykey(String userid);
}

然后在ycshop-manager-service模块下创建包cn.yuechenc.ycshop.manager.service.impl
在此包里创建UserServiceImpl实现了,实现UserService接口

package cn.yuechenc.ycshop.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.yuechenc.manager.dao.mapper.UserMapper;
import cn.yuechenc.pojo.User;
import cn.yuechenc.ycshop.manager.interfaces.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectUserByPrimarykey(String userid) {
        return userMapper.selectByPrimaryKey(userid);
    }
}

此时我们的service层就算完成了,接下来,我们对修改过得项目进行install,
此时我们会发现,在对ycshop-manager进行install的时候回报一个错,如图:
分布式商城项目–编写UserService测试接口
此处请参考下面的解决方法:
Maven Install报错:Perhaps you are running on a JRE rather than a JDK?
解决之后,对ycshop-manager工程进行build,看到下图,表示已经成功将接口暴露到dubbo服务了
分布式商城项目–编写UserService测试接口
下面,我们就需要在ycshop-manager-web工程中去接收测试能否获取到接口服务

编写controller

在web模块下编写UserController

package cn.yuechenc.ycshop.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.yuechenc.pojo.User;
import cn.yuechenc.ycshop.manager.interfaces.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    @ResponseBody
    private User getUser(){
        return userService.selectUserByPrimarykey("1a");
    }
}

此处在注入service是会注入不进来,是因为之前搭建工程是没有在web工程的pom文件中加入对接口的依赖,补充:

<!-- 对 dao 的依赖 -->
        <dependency>
            <groupId>cn.yuechenc</groupId>
            <artifactId>ycshop-manager-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 对接口的依赖 -->
        <dependency>
            <groupId>cn.yuechenc</groupId>
            <artifactId>ycshop-manager-interfaces</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

现在保持依次[build]
ycshop-manager
和ycshop-manager-web
在浏览器输入http://localhost:8080/user/getUser进行访问,报如图错误
分布式商城项目–编写UserService测试接口
是因为dubbo服务之间通行时会将信息序列化之后以流的形式传输,所以就要求传输的对象是可以序列化的,此处只需让我们的pojo类实现Serializable接口即可
分布式商城项目–编写UserService测试接口
在此install项目并运行,并且访问http://localhost:8080/user/getUser
可以在浏览器中看到如下信息
分布式商城项目–编写UserService测试接口
到这里,我们所有的后台项目环境就算是搭建好了,接下来就是业务逻辑的开发
我们下节见。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI