0

I just started learning Spring Boot by reading the book Spring Boot in Action and I am copying the examples of the book, trying to run them myself but I have a problem using findOne. The project is supposed to be a simple Reading List. Here is the code :

The interface:

public interface ReaderRepository extends JpaRepository<Reader, String> {
}

The Reader class:

@Entity
public class Reader implements UserDetails {
    private static final long serialVersionUID = 1L;

    @Id
    private String username;
    private String fullname;
    private String password;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getFullname() {
        return fullname;
    }
    public void setFullname(String fullname) {
        this.fullname = fullname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    // UserDetails methods
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Arrays.asList(new SimpleGrantedAuthority("READER"));
    }
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @Override
    public boolean isEnabled() {
        return true;
    }
}

The SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private ReaderRepository readerRepository;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").access("hasRole('READER')")
                .antMatchers("/**").permitAll()
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=true");
    }

    @Override
    protected void configure(
            AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(new UserDetailsService() {
                    @Override
                    public UserDetails loadUserByUsername(String username)
                            throws UsernameNotFoundException {
                        return readerRepository.findOne(username);
                    }
                });
    }
}

And the error:

error-image

I hope someone can help me because I am lost. Thank you.

Michalis G.
  • 21
  • 1
  • 6
  • Check your imports on ReaderRepository you might been importing the wrong 'Reader' – Jorge C Sep 26 '18 at 09:32
  • Code seems to fine. As @JorgeC suggested just check with your import. Probably you can perform clean-build. – Shaunak Patel Sep 26 '18 at 09:38
  • Thank you for your answers. I changed my import, still same error. When i try to build i get this Error:(38, 48) java: method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor cannot be applied to given types; required: org.springframework.data.domain.Example found: java.lang.String reason: cannot infer type-variable(s) S (argument mismatch; java.lang.String cannot be converted to org.springframework.data.domain.Example) – Michalis G. Sep 26 '18 at 09:42
  • which spring boot version are you using? – Shaunak Patel Sep 26 '18 at 10:29
  • I am using Spring Boot 2.0.5 – Michalis G. Sep 26 '18 at 10:51
  • I might be wrong but, `@Id` annotation by default expects a numeric type, `int`, `long` or `short` as explained [here](https://stackoverflow.com/questions/10041938/how-to-choose-the-id-generation-strategy-when-using-jpa-and-hibernate). You can use `String` in `@Id` annotation if it is an `UUID` as explained [here](https://stackoverflow.com/questions/18622716/how-to-use-id-with-string-type-in-jpa-hibernate) – raidensan Sep 26 '18 at 11:19

1 Answers1

0

java: method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T> cannot be applied to given types; required: org.springframework.data.domain.Example<S> found: java.lang.String reason: cannot infer type-variable(s) S (argument mismatch; java.lang.String cannot be converted to org.springframework.data.domain.Example<S>)

It's very straight forward. You need to pass Example object here. Here is a code.

Reader  r = new Reader().
r.setUserName("somename");

readerRepository.findOne(Example.of(r));
Shaunak Patel
  • 1,581
  • 11
  • 14