Spring数据JPA微服务中的资源映射问题

时间:2017-12-13 13:21:29

标签: spring-boot spring-data-jpa

我正在尝试在我的控制器中使用findAll()方法。我写了一个存储库,模型和控制器作为例子。但是,当我运行的网址显示“此应用程序没有显示/错误的显式映射时,所以您将此视为后备”

我的控制器是:

@RestController
public class WebController {

@Autowired
UsersRepository userRepo;

@GetMapping("/loadAuthUsers")
public List<Users> checkLogin() {
    return (List<Users>) userRepo.findAll();
}
}

我的模特是,

@Entity
@Table(name = "users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "username")
    public String username;

    @Column(name = "password")
    public String password;

    @Column(name = "uia_token")
    public String uia_token;

    public Integer getId() {
        return id;
    }
    public void setId(Integer 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 getUia_token() {
        return uia_token;
    }

    public void setUia_token(String uia_token) {
        this.uia_token = uia_token;
    }
    public User() {
    }
    public User(Integer userId, String username, String password , String 
uia_token) {
        this.username = username;
        this.password = password;
        this.uia_token = uia_token;
    }
}

我的存储库是,

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {

}

我的application.properties包含,

spring.datasource.url=jdbc:postgresql://localhost/espace
spring.datasource.username=postgres
spring.datasource.password=postgresql
spring.jpa.generate-ddl=true
server.port=8081

我的主要课程是,

@SpringBootApplication
@Configuration
@ComponentScan("com.milletech.services.repository")
public class EspaceCheckAuthenticationApplication {

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

任何人都可以帮忙解决这个映射问题吗?

1 个答案:

答案 0 :(得分:1)

根据日志,它会自动连接错误。

您需要使用 @Repository 注释 UserRepository 类,以便进行自动装配。

工作代码为:

@SpringBootApplication
@EnableJpaRepositories(considerNestedRepositories = true)
public class MyApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyApplication.class, args);
    }
}

在Spring应用程序类中添加注释 @EnableJpaRepositories

Kernel.php