|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
工程正常运行 正确的数据被正确的写入数据库 可是返回信息总是出错
运行工程一切正常
使用postman 发生注册请求 用户名和用户请求被正常写入数据库
但是postman的返回信息如下
{"code":null,"message":null,"data":null}
正常情况下 如果成功注册的话 返回信息应该是
{"code":0,"message":操作成功,"data":null}
如果操作不成功的话 返回信息应该是
{"code":1,"message":用户名已被占用,"data":null}
或者
{"code":1,"message":参数不合法,"data":null}
我的reslut 类代码如下
package com.itheima.big_evnet.pojo;
public class Result<T> {
private Integer code;//业务状态码 0-成功 1-失败
private String message;//提示信息
private T data;//响应数据
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Result() {
}
public Result(Integer code, String message, T data) {
// TODO Auto-generated constructor stub
}
public static <E> Result<E> success(E data) {
return new Result<> (0, "操作成功", null);
}
public static Result success() {
return new Result(0, "操作成功", null);
}
public static Result error(String message) {
return new Result(1, message, null);
}
}
UserController类代码如下
package com.itheima.big_evnet.controller;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
import com.itheima.big_evnet.pojo.Result;
import com.itheima.big_evnet.pojo.User;
import com.itheima.big_evnet.service.UserService;
@Mapper
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public Result register(String username, String password) {
if(username!=null && username.length()>=5 && username.length()<=16 && password!=null && password.length()>=5 && password.length()<=16) {
User u = userService.findByUserName(username);
if (u==null) {
userService.register(username, password);
return Result.success();
}else {
return Result.error("用户名已被占用");
}
}else {
return Result.error("参数不合法");
}
}
}
请问我的代码有问题吗? 为什么无法实现我的要求的功能
你在Result类的构造函数中没有给code、message和data赋值,导致返回的结果中这些字段都是空的。你可以修改一下Result类的构造函数,给这些字段赋上相应的值:
- public Result(Integer code, String message, T data) {
- this.code = code;
- this.message = message;
- this.data = data;
- }
复制代码
另外,在UserController类的register方法中,你只对username长度进行了判断,但没有对其他条件进行判断。你可以使用if-else语句来判断不同条件下的返回结果:
- @PostMapping("/register")
- public Result register(String username, String password) {
- if (username != null && username.length() >= 5 && password != null && password.length() >= 5) {
- // 注册成功逻辑
- // ...
- return Result.success(); // 返回操作成功的结果
- } else if (username != null && username.length() < 5) {
- return Result.error("用户名长度不能小于5"); // 返回用户名长度不合法的结果
- } else if (password != null && password.length() < 5) {
- return Result.error("密码长度不能小于5"); // 返回密码长度不合法的结果
- } else {
- return Result.error("参数不合法"); // 返回其他参数不合法的结果
- }
- }
复制代码
这样,当注册成功时返回 - {"code":0,"message":操作成功,"data":null}
复制代码,当用户名长度不合法时返回 - {"code":1,"message":用户名长度不能小于5,"data":null}
复制代码,当密码长度不合法时返回 - {"code":1,"message":密码长度不能小于5,"data":null}
复制代码,当其他参数不合法时返回 - {"code":1,"message":参数不合法,"data":null}
复制代码。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|