前提・実現したいこと
Spring BootでRest Webサービスの開発を行なっています。
データを1件取得するAPIを実行したところエラーとなってしまいました。
発生している問題・エラーメッセージ
アプリケーションを起動後getメソッドを行うとエラーが返ってくる
$curl http://localhost:8080/api/customers/1 -i -XGET HTTP/1.1 500 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Thu, 31 May 2018 09:20:04 GMT Connection: close {"timestamp":"2018-05-31T09:20:04.234+0000","status":500,"error":"Internal Server Error","message":"Type definition error: [simple type, class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.example.hajibootrest.Customer_$$_jvstaf1_0[\"handler\"])","path":"/api/customers/1"}
該当のソースコード
以下クラスです。
HajibootRestApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HajibootRestApplication { public static void main(String[] args) { SpringApplication.run(HajibootRestApplication.class, args); } }
CustomerService.java
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class CustomerService { @Autowired CustomerRepository customerRepository; public List<Customer> findAll(){ return customerRepository.findAllOrderByName(); } public Customer findOne(Integer id) { return customerRepository.getOne(id); } public Customer create(Customer customer) { return customerRepository.save(customer); } public Customer update(Customer customer) { return customerRepository.save(customer); } public void delete(Integer id) { Customer customer = customerRepository.getOne(id); customerRepository.delete(customer); } }
CustomerRestController.java
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api/customers") public class CustomerRestController { @Autowired CustomerService customerService; @GetMapping List<Customer> getCustomers(){ List<Customer> customers = customerService.findAll(); return customers; } @GetMapping(path = "{id}") Customer getCustomer(@PathVariable Integer id) { Customer customer = customerService.findOne(id); return customer; } }
CustomerRepository.java
import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; public interface CustomerRepository extends JpaRepository<Customer, Integer> { @Query(value = "SELECT x FROM Customer x ORDER BY x.firstName, x.lastName") List<Customer> findAllOrderByName(); }
Customer.java
import javax.persistence.*; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Entity @Table(name = "customers") @Data @NoArgsConstructor @AllArgsConstructor public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(nullable = false) private String firstName; @Column(nullable = false) private String lastName; }
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> <groupId>com.example</groupId> <artifactId>hajiboot-rest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hajiboot-rest</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy spring.datasource.url=jdbc:log4jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=sa spring.datasource.password= logging.level.jdbc=OFF logging.level.jdbc.sqltiming=DEBUG
data.sql
INSERT INTO customers(first_name,last_name) VALUES('Nobita', 'Nobi'); INSERT INTO customers(first_name,last_name) VALUES('Takeshi', 'Goda'); INSERT INTO customers(first_name,last_name) VALUES('Suneo', 'Honekawa'); INSERT INTO customers(first_name,last_name) VALUES('Shizuka','Minamoto');
最後のdata.sqlで先に情報をDBに入れておきます。
試したこと
全件取得の場合はしっかりJSONが返ってきました
$curl http://localhost:8080/api/customers -i -XGET
そのため$curl http://localhost:8080/api/customers/1 -i -XGET
のRestControllerに問題があるのではと思いました。findOneメソッドに以上があるのではないかとAPIリファレンスを読んだのですが、問題なさそうでした。
補足情報(FW/ツールのバージョンなど)
spring bootのバージョン:2.0.1
使っているDB:H2
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー