我正在遵循一个教程的春季启动示例,该示例实现了Restful服务以从Database表中获取人员列表,但是当我在IE上调用http://localhost:8080/person时遇到错误:找不到用于的转换器类型的返回值:类java.util.ArrayList。 到目前为止,在我上完课之后,我尝试从关于stackoverflow的建议中添加Jackson依赖项,对任何帮助都感激。 SpringbootApplication:
package ben;
@SpringBootApplication
@EnableAutoConfiguration
public class SpringBootWebApplication
{
public static void main (String [] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
BDE4类:
package ben.models;
@Entity
@Table(name="BDE4")
public class Bde
{
@Id
@Column
private int id;
@Column
private int numero;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getNumero()
{
return numero;
}
public void setNumero(int numero)
{
this.numero = numero;
}
}
PrsonRepository接口:
package ben.repository;
@Repository
public interface PersonRepository extends CrudRepository<Bde, Integer> {
}
PersonService接口:
package ben.services;
public interface PersonService
{
public List <Bde> findAll();
}
PersonServiceImpl类:
package ben.services;
@Service
public class PersonServiceImpl implements PersonService
{
@Autowired
private PersonRepository personRepository;
@Override
public List<Bde> findAll()
{
System.out.println("PersonServiceImpl");
return (List<Bde>) personRepository.findAll();
}
}
PersonRest类:
package ben.web;
@RestController
public class PersonRest
{
@Autowired
private PersonService personService;
@RequestMapping("/person")
public List <Bde> findAll() {
List <Bde> persons=(List<Bde>) personService.findAll();
for (Bde pers:persons)
{
System.out.println("id"+pers.getId() +" numero"+pers.getNumero());
}
return persons;
}
}
POM.XML:依赖项
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BenId</groupId>
<artifactId>SpringBoot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
<start-class>ben.SpringBootWebApplication</start-class>
<java.version>1.8</java.version>
<oracle.driver.version>12.1.0.2</oracle.driver.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- <version>2.8.4.RELEASE</version> -->
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-thymeleaf</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.52</version>
<!-- <scope>provided</scope> -->
<!-- <version></version> -->
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc7</artifactId>
<version>${oracle.driver.version}</version>
</dependency>
<!-- <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<mainClass>ben.SpringBootWebApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
应用程序属性:
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
spring.datasource.url=jdbc:oracle:thin:@12.53.2.202:1521:xe
spring.datasource.username=hr
spring.datasource.password=hr
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
答案 0 :(得分:0)
尝试:
@GetMapping("/person")
public List <Bde> findAll() {
List <Bde> persons=personService.findAll();
for (Bde pers:persons)
{
System.out.println("id"+pers.getId() +" numero"+pers.getNumero());
}
return persons;
}
并且在:类Bde
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
答案 1 :(得分:0)
我刚刚设置了您的项目,并且它的工作正常,我刚刚更改了pom.xml和application.properties。此外,我正在使用MySQL作为数据库。这是pom.xml和属性文件。而且,也没有更改其他文件中的单个单词。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
server.port = 8080