Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 用户登录时的CrudRepository抛出数据不匹配_Spring Boot_Jpa - Fatal编程技术网

Spring boot 用户登录时的CrudRepository抛出数据不匹配

Spring boot 用户登录时的CrudRepository抛出数据不匹配,spring-boot,jpa,Spring Boot,Jpa,我正在尝试为我的webapp设置用户端点。(/user/{id}) 我有一个thymeleaf登录表单,它调用一个getUser端点,该端点通过电子邮件和密码扫描用户数据库中的用户。一旦找到用户,它将重定向到/user/{id}。在/user/{id}的请求映射中,它使用UsersService调用调用findById(String)的UserRepository id的值是长的。crud存储库有一个findById(String String)方法,它应该将它映射到一个long,因为它在通配符

我正在尝试为我的webapp设置用户端点。(/user/{id}) 我有一个thymeleaf登录表单,它调用一个getUser端点,该端点通过电子邮件和密码扫描用户数据库中的用户。一旦找到用户,它将重定向到/user/{id}。在/user/{id}的请求映射中,它使用UsersService调用调用findById(String)的UserRepository

id的值是长的。crud存储库有一个findById(String String)方法,它应该将它映射到一个long,因为它在通配符中被定义为一个类型。 我给这个方法一个字符串,因为它告诉我它的数据类型不正确

如何按id查询用户?为什么会抛出这个不兼容的类型错误



// thymeleaf form with get to /user/getUser endpoint

<form action="#" method="GET" th:action="@{/user/getUser}" th:object="${loginRequestModel}">
<p>Email: <input th:field="*{email}" type="text"/></p>
<p>Password: <input th:field="*{password}" type="text"/></p>
<p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>

// controller class with request mapping to /user 

   @GetMapping(value = "/{id}")
    @ResponseBody
    public String userUploads(@PathVariable("id") String id) {
        log.info("userUploads endpoint called<<");
        String returnValue;
        try {
            UserEntity userEntity = usersService.getUserById(id);

            returnValue = "this account belongs to: " +
                    userEntity.getFirstName() + userEntity.getLastName();

            log.info("returning new response entity<<");
            return returnValue;

        } catch (Exception e){
            e.printStackTrace();
        }
            return "Couldn't load profile!";
        }


@GetMapping("/getUser")
public String getUser(@ModelAttribute LoginRequestModel loginRequestModel) {
        String userEmail = loginRequestModel.getEmail();
        UserEntity userEntity = usersService.getUserDetailsByEmail(userEmail);
        String id = String.valueOf(userEntity.getId());

        return "redirect:/user/" + id;

    }

// UsersService which contains the autowired CrudRepository

    @Override
    public UserEntity getUserDetailsByEmail(String email) {
        return userRepository.findByEmail(email);
    }


    @Override
    public UserEntity getUserById(String userId) {
        return userRepository.findById(userId);
    }


// crud repository

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<UserEntity, Long> {

    UserEntity findByEmail(String email);
    UserEntity findByUsername(String username);
    UserEntity findById(String id);



}



// my user entity class

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "WEB_USERS")
@Data
public class UserEntity implements Serializable {

    @Getter(AccessLevel.NONE)
    public static final long serialVersionUID = -2056698267929616904L;

    protected UserEntity() {
    }

    public UserEntity(String email, String username, String password, String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.username = username;
        this.password = password;

    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long id;

    @Column(name = "first_name")
    public String firstName;

    @Column(name = "last_name")
    public String lastName;

    @Column(unique = true)
    public String email;

    @Column
    public String username;

    @Column
    public String password;

    @Override
    public String toString() {


        String toString =
                "\n\n\t > User Entity" +
                        "\n\n\t > Id:" + id +
                        "\n\n\t > First Name:" + firstName +
                        "\n\n\t > Last Name:" + lastName +
                        "\n\n\t > Email:" + email +
                        "\n\n\t > Username:" + username +
                        "\n\n\t > Password:" + password;

        return toString;




    }
}

//带有get to/user/getUser端点的thymeleaf表单
电邮:

密码:

//请求映射到/user的控制器类 @GetMapping(value=“/{id}”) @应答器 公共字符串用户上载(@PathVariable(“id”)字符串id){
log.info(“userUploads endpoint”称为,因为实体类中的字段
id
为Long类型,控制器中的函数为String类型,并且UserService中的函数findUserById中的参数id也为String类型

   @GetMapping(value = "/{id}")
    @ResponseBody
    public String userUploads(@PathVariable("id") String id) {
        // ...
    }
解决方案:将控制器中的Pathvariable id修改为Long。并修改与该控制器一起使用的相关函数(例如:userService…)


但即使是在官方页面上,这也是它告诉您要做的。我遵循了教程,在教程中,我以完全相同的方式成功地做到了这一点。将id作为字符串可以吗?我读过的所有地方都说要使用Long和search by string值。我尝试将crud存储库更改为have,但它抛出了一个错误,因为重写的方法s this:Optional findById(ID var1);@jjavaggdev我可以在UserService中看到您的函数吗?[http-nio-8080-exec-3]错误o.a.c.c.c.[[dispatcherServlet]-Servlet.service()用于路径[]上下文中的Servlet[dispatcherServlet]引发异常[请求处理失败;嵌套异常为org.springframework.orm.jpa.JpaSystemException:ids的未知整数数据类型:java.lang.String;嵌套异常为org.hibernate.id.IdentifierGenerationException:ids的未知整数数据类型:java.lang.String]使用根本原因org.hibernate.id.IdentifierGenerationException:ids的未知整型数据类型:java.lang.StringThat在将all更改为Strings后如果我将Crudepository更改为按长类型搜索,则强制我更改为:可选findById(长id);
   @GetMapping(value = "/{id}")
    @ResponseBody
    public String userUploads(@PathVariable("id") String id) {
        // ...
    }
   @GetMapping(value = "/{id}")
    @ResponseBody
    public String userUploads(@PathVariable("id") Long id) {
        // ...
    }