如何创建登录和注册表单并在 Java 和前端 js 中为其后端创建测试?
Posted
技术标签:
【中文标题】如何创建登录和注册表单并在 Java 和前端 js 中为其后端创建测试?【英文标题】:how to create sign-in and sign-up form & create a test for it backend in Java and frontend js? 【发布时间】:2022-01-24 04:00:27 【问题描述】:我想学,怎么做一个有这个功能的项目。 (登录和注册) 这些是我目前上过的课程。
这是我也有用户类的实体
@Entity(name = "Users")
public class UserEntity
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id" ,nullable = false)
private int id;
@Column(name = "first_name" )
private String firstname;
@Column(name = "last_name" )
private String lastname;
@Column(name = "Email" )
private String email;
protected UserEntity()
public UserEntity( String firstname, String lastname, String email)
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
这是方法所在的服务类
@Service
public class UserService
private final UserRepository usersRepository;
public UserService(UserRepository usersRepository)
this.usersRepository = usersRepository;
public List<User> findAll ()
List<UserEntity> users = usersRepository.findAll();
return users.stream().map(this::entityToUser).collect(Collectors.toList());
public User findUserbyId(int id)
var userentity = usersRepository.findById(id);
return userentity.map(this::entityToUser).orElse(null);
public User update(int id , UserCreateRequest request)
var userEntityOptional = usersRepository.findById(id);
if(userEntityOptional.isEmpty())
return null;
var userEntity = userEntityOptional.get();
userEntity.setFirstname(request.getFirstname());
userEntity.setLastname(request.getLastname());
userEntity.setEmail(request.getEmail());
usersRepository.save(userEntity);
return entityToUser(userEntity);
public boolean deleteById(int id)
if (!usersRepository.existsById(id))
return false;
usersRepository.existsById(id);
return true;
public User create(UserCreateRequest request)
var userEntity = new UserEntity(request.getFirstname(),request.getLastname(),request.getEmail());
userEntity = usersRepository.save(userEntity);
return entityToUser(userEntity);
public User entityToUser(UserEntity userEntity)
return new User (
userEntity.getId(),
userEntity.getFirstname(),
userEntity.getLastname(),
userEntity.getEmail());
这里是用户控制器
@RestController
public class UserRestController
private final UserService userService;
public UserRestController(UserService userService)
this.userService = userService;
@GetMapping(path = "/api/v1/users")
public ResponseEntity<List<User>> fetchUsers()
return ResponseEntity.ok(userService.findAll()) ;
我想知道我需要什么才能以最佳方式正确使用此功能 有哪些必要的步骤?
【问题讨论】:
在进行改进之前,首先找出您在使用这种方法时面临的问题,一旦您发现您将清楚地知道您需要在哪一侧进行改进 从给定的代码中我可以建议您可以使用 @Autowired 功能而不是将其设置为构造函数 【参考方案1】:@Service
public class UserService
//make this Autowired
@Autowired
private UserRepository usersRepository;
public List<User> findAll ()
List<UserEntity> users = usersRepository.findAll();
return users.stream().map(this::entityToUser).collect(Collectors.toList());
public User findUserbyId(int id)
var userentity = usersRepository.findById(id);
return userentity.map(this::entityToUser).orElse(null);
public User update(int id , UserCreateRequest request)
var userEntityOptional = usersRepository.findById(id);
if(userEntityOptional.isEmpty())
return null;
var userEntity = userEntityOptional.get();
userEntity.setFirstname(request.getFirstname());
userEntity.setLastname(request.getLastname());
userEntity.setEmail(request.getEmail());
usersRepository.save(userEntity);
return entityToUser(userEntity);
public boolean deleteById(int id)
if (!usersRepository.existsById(id))
return false;
usersRepository.existsById(id);
return true;
public User create(UserCreateRequest request)
var userEntity = new UserEntity(request.getFirstname(),request.getLastname(),request.getEmail());
userEntity = usersRepository.save(userEntity);
return entityToUser(userEntity);
public User entityToUser(UserEntity userEntity)
return new User (
userEntity.getId(),
userEntity.getFirstname(),
userEntity.getLastname(),
userEntity.getEmail());
这里是用户控制器
@RestController
public class UserRestController
//make this Autowired
@Autowired
private UserService userService;
@GetMapping(path = "/api/v1/users")
public ResponseEntity<List<User>> fetchUsers()
return ResponseEntity.ok(userService.findAll()) ;
点击此处了解更多关于@Autowired https://www.baeldung.com/spring-autowire
【讨论】:
以上是关于如何创建登录和注册表单并在 Java 和前端 js 中为其后端创建测试?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用模板在 WooCommerce 的两个不同页面上显示登录和注册表单
高分求教:如何实现java web 按钮点击弹出登陆界面,并登陆