java - 无法在 SPRING 中将类型 'java.lang.String' 的值转换为所需类型 'java.lang.Long'

标签 java spring jpa

我想按 ID 和“角色”对项目进行排序:

当我引用“角色”时,Spring 认为我想引用 Long 类型的 ID,并在逻辑上崩溃。我尝试了一些与类似错误相关的选项,但没有一个对我有用...这是代码:

//My repository 

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

    Optional<Employee> findById(Long  id);
    List<Employee> findByRole(RoleList role);

}

public class Employee {

    private @Id @GeneratedValue Long id;
    private String name;
    private RoleList role;

    Employee() {
    }

    public Employee(String name, RoleList role) {
        this.name = name;
        this.role = role;

    }

//Getter and setters.............

    @Override
    public String toString() {
        return "Employee [id = " + getId() + ", name = " + getName() + ", role = " + getRole().getNameRole() + 
                ", with salary of = " + getRole().getSalaryRole() + "]";
    }

}
public enum RoleList {

    BURGLAR("burglar", 1500),
    THIEF("thief", 2000),
    MAGE("mage", 3500);

    private String role;
    private int salaryRole;

    private RoleList(String role, int salaryRole) {
        this.role = role;
        this.salaryRole = salaryRole;
    }

    public int getSalaryRole() {
        return salaryRole;
    }

    public String getNameRole() {
        return role;
    }
//initial database

class LoadDatabase {

  @Bean
  CommandLineRunner initDatabase(EmployeeRepository repository) {
    return args -> {
      log.info("Preloading " + repository.save(new Employee("Bilbo Baggins", RoleList.BURGLAR)));
      log.info("Preloading " + repository.save(new Employee("Frodo Baggins", RoleList.THIEF)));
      log.info("Preloading " + repository.save(new Employee("Gandalf the Grey", RoleList.MAGE)));


    };
  }
}
// Single item by id

  @GetMapping("/employees/{id}")
  Employee one(@PathVariable Long id) {

    return repository.findById(id).orElseThrow(() -> new EmployeeNotFoundException(id));
  }

//...这按预期工作...然后我几乎没有选择,这些都不起作用...

// Items by role

@GetMapping("employees/role/{role}")
List<Employee> getRoles(@PathVariable (value="role")String role) {

    List<Employee> listRolesEmployees = repository.findByRole(RoleList.valueOf(role));
    return listRolesEmployees;
}

//...或者...

@RequestMapping(value = "employee/{role}", method = RequestMethod.GET)
List<Employee> getRoles(@PathVariable String role) {

    List<Employee> listRolesEmployees = repository.findByRole(RoleList.valueOf(role));
    return listRolesEmployees;
}

//...或者...

  @RequestMapping(method = RequestMethod.GET)
  public List<Employee> getRoles(@RequestParam(value="role") String role) {
        List<Employee> listRolesEmployees = repository.findByRole(RoleList.valueOf(role));
        return listRolesEmployees;
  }

抱歉我的英语不好,谢谢!

最佳答案

在您的端点中,您使用:

@RequestMapping(value = "employee/{role}", method = RequestMethod.GET)

但是在请求中http://localhost:8080/employees/BURGLAR这不是同一个路径(额外的字符s)。

你陷入了错误的终点:

@GetMapping("/employees/{id}")
  Employee one(@PathVariable Long id) {

但是BURGLAR不是Long。 这会导致转换错误。

关于java - 无法在 SPRING 中将类型 'java.lang.String' 的值转换为所需类型 'java.lang.Long',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327296/

相关文章:

java - native 镜像的 Spring Boot 构建镜像失败

java - I18N 属性上的 org.springframework.context.NoSuchMessageException

java - 有人可以解释 JPA 和 Hibernate 中的 mappedBy 吗?

java - 每次运行 Junit 测试时实体管理器都为空

java - 我如何构建一个系统来实现,接口(interface)中的 protected 方法会做什么?

java - 如何使用 Spock 在 Spring Boot 过滤器中模拟服务或 stub 服务方法?

java - 哪个 api/sdk 在 Android 平台上从帐户/页面获取图像简单且有用?

java - 如何从android中的自定义 ListView 中选择具有id的 TextView ?

java - 如何在 Spring 中使用枚举设置 int 属性?

java - Worklight 6 升级失控