Код: Выделить всё
package com.example.arvrapp.repository;
import com.example.arvrapp.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository {
User findByUsername(String username);
}
Here is my usercontroller.java:
package com.example.arvrapp.controller;
import com.example.arvrapp.model.User;
import com.example.arvrapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@PostMapping("/signup")
public User signUp(@RequestBody User user) {
return userRepository.save(user);
}
@PostMapping("/login")
public String login(@RequestBody User user) {
User existingUser = userRepository.findByUsername(user.getUsername());
if (existingUser != null && existingUser.getPassword().equals(user.getPassword())) {
return "Login successful!";
} else {
return "Invalid credentials!";
}
}
}
Код: Выделить всё
User.java:
package com.example.arvrapp.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String email;
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Код: Выделить всё
package com.example.arvrapp.service;
import com.example.arvrapp.model.User;
import com.example.arvrapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User registerUser(User user) {
return userRepository.save(user);
}
public User findUserByUsername(String username) {
return userRepository.findByUsername(username);
Я получаю следующее:
Страница ошибки Whitelabel:
Это приложение не имеет явного сопоставления для /error, поэтому вы видите это как запасной вариант.
Сб, 13 июля 13 г. :54:21 IST 2024.
Произошла непредвиденная ошибка (тип=Не найден, статус=404).
Подробнее здесь: https://stackoverflow.com/questions/787 ... error-page
Мобильная версия