人机对话
概述
基于Springboot Theamleaf的人机对话,使用Maven进行包管理。主要功能包括:登录拦截器、人机对话。
环境配置
登录控制器
@Controller
@RequestMapping(value = "/user")
public class LoginController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping(value = "/login")
public String login(UserInfo userInfo, HttpSession session) {
if (!StringUtils.isEmpty(userInfo.getUsername()) && !StringUtils.isEmpty(userInfo.getPassword())) {
UserInfo isUserExist = userService.isUserExist(userInfo);
if (null == isUserExist || null == isUserExist.getUsername()) {
return JSON.toJSONString(NormalResponse.create()
.state(10001)
.message("Incorrect Username Or Password"));
} else {
session.setAttribute("loginUser", isUserExist.getUsername());
return JSON.toJSONString(NormalResponse.create()
.state(10000)
.message("Login Success")
.put("username", isUserExist.getUsername())
.put("portrait", isUserExist.getPortrait())
.put("id", isUserExist.getId())
.put("type", isUserExist.getType()));
}
} else {
return JSON.toJSONString(NormalResponse.create()
.state(10002)
.message("Username Or Password Is Empty"));
}
}
}
对话控制器
@Controller
@RequestMapping(value = "/message")
public class MessageController {
@Autowired
private MessageService messageService;
@ResponseBody
@RequestMapping(value = "/getAnswer")
public String getAnswer(String question) {
String answer = messageService.getAnswer(question);
return JSON.toJSONString(NormalResponse.create()
.state(20000)
.message(answer));
}
}
运行配置
-
首先安装Mysql5.7,设置用户名为root,密码为root,并保证其在运行状态,并执行sql文件导入数据。
-
然后再配置Maven到环境变量中,在源代码目录下运行
-
使用浏览器访问http://localhost:8080即可进入系统。
功能展示
1. 首页登陆
2.1 人机对话
2.2 人机对话
来源:oschina
链接:https://my.oschina.net/u/4324735/blog/4807707