【问题标题】:The method asList(T...) in the type Arrays is not applicable for the arguments (Role, Role)Arrays 类型中的 asList(T...) 方法不适用于参数 (Role, Role)
【发布时间】:2018-08-31 17:37:11
【问题描述】:

如果我实施得当,我正在尝试使用 Postman 测试 OAuth 2.0 授权。

我正在通过邮递员发送这个作为回报的预期令牌

http://localhost:8080/oauth/token/grant_type=password&username=user&password=user

但我在这部分代码中有错误:

@SpringBootApplication
public class KamehouseApplication {

    public static void main(String[] args) {
        SpringApplication.run(KamehouseApplication.class, args);

    }

    @Autowired
    public void configure(AuthenticationManagerBuilder auth, UserRepository repo, User user) throws Exception {
        if (repo.count() == 0)
            user = (new User("user", // username
                    "user", // password
                    Arrays.asList(new Role("USER"), new Role("ACTUATOR"))));// roles

        repo.save(user);

        auth.userDetailsService(s -> new CustomUserDetails(repo.findByUsername(s)));
    }
}

红色下划线部分是

Arrays.asList(new Role("USER"), new Role("ACTUATOR"))));

The method asList(T...) in the type Arrays is not applicable for the arguments (Role, Role)

这是我的用户类

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue
    public int id;

    public String firstname;
    public String lastname;
    public String username;
    public String password;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Role> roles;

    public User() {
    }

    //Getters/Setters

【问题讨论】:

  • 您确定要导入java.util.Arrays 而不是其他Arrays 类吗? User 构造函数的签名是什么?
  • java版本是多少?
  • @lealceldeiro java版本“1.8.0_131”
  • 也许不是问题,但我没有看到 User(String username, String password, List roles) 的任何构造函数...只有 User()
  • 如果在另一行你用“Long”代替角色为我做List&lt;Role&gt; roles = Arrays.asList(new Role("USER"), new Role("ACTUATOR"),你会得到同样的结果吗?您也没有带参数的 User 构造函数。也不需要在user = (new User(...)); 处使用括号括起来,还从方法签名中删除用户用户,您从未使用过传入的值。

标签: java spring oauth-2.0


【解决方案1】:

解决方案

我改变了构造函数

public Role() {

}

public Role(String string) {

}

到我的 Role.java 文件并添加了

public User(String username, String password, List roles) {
}

到我的 User.java,现在它工作得很好。

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2014-05-09
    • 2013-03-20
    相关资源
    最近更新 更多