Spring : problème de récupération de donnée depuis la base de données.
Après plusieurs jours de galère, j'ai réussi à configurer sqlite3 pour l'utiliser avec hibernate et spring. Pour tester j'ai fait un petit programme avec une seule table dans ma base de données.Le problème, j'ai une page blanche dans le navigateur si j'exécute la classe principale et que j'accède à cette URL "localhost: 8080/getAllTypes" pourtant j'ai 2 enregistrements(qui devraient être retournés) dans ma table. Voici mes différents scripts:
Entity :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Entity
@Table(name="types")
public class Type {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
//@GeneratedValue(strategy = GenerationType.TABLE)
private long idType;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public long getIdType() {
return idType;
}
public void setIdType(long idType) {
this.idType = idType;
}
} |
CrudRepository :
Code:
1 2 3 4 5
|
import org.springframework.data.repository.CrudRepository;
public interface TypeRepository extends CrudRepository<Type,Long> {
Type findByDescription(String description);
} |
L'interface service
Code:
1 2 3 4 5
|
import java.util.Collection;
public interface TypeService {
Collection<Type> getAllTypes();
} |
Implementation du service:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import java.util.Collection;
import javax.annotation.Resource;
import org.apache.commons.collections4.IteratorUtils;
import org.springframework.stereotype.Service;
@Service(value="typeService")
public class TypeServiceImpl implements TypeService {
@Resource
private TypeRepository typeRepository;
@Override
public Collection<Type> getAllTypes() {
return IteratorUtils.toList(this.typeRepository.findAll().iterator());
}
} |
Le contrôleur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TypeController {
@Resource
private TypeService typeService;
@RequestMapping(value= "/getAllTypes", method = RequestMethod.GET)
public Collection<Type> getAllTypes() {
return this.typeService.getAllTypes();
}
} |
application.yml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
spring:
profile:dev
datasource.url:jdbc:sqlite:C:\Users\user pc\Desktop\PDF\testrest.db
datasource.driverClassName:org.sqlite.JDBC
jpa:
hibernate:
hbm2ddl.auto:update
properties:
hibernate:
dialect:org.hibernate.dialect.SQLiteDialect
datasource:
url:jdbc:sqlite:C:\Users\user pc\Desktop\PDF\testrest.db
username:user
password:user
driverClassName:org.sqlite.JDBC |
Et enfin, la classe principale :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class MainLauncher {
public static void main(String[] args) {
SpringApplication.run(MainLauncher.class, args);
}
} |
Je vous remercie d'avance pour vos aides.
N.B: je signale que je n'ai aucun message d'erreur.