Spring 数据 Rest returns 使用数据访问 table 时的 http 500
Spring data Rest returns http 500 when accessing table with data
我正在尝试设置 spring-data-rest 服务。我在 Eclipse 中使用 spring-data-rest 和 hibernate。服务工作正常(返回 HAL + JSON),只要我在 table 中没有数据,我正在尝试从中获取数据。
当我尝试从 table 获得包含数据的响应时,我在浏览器中得到空白页面,eclipse 仅显示休眠查询但未显示异常,并且仅在 curl 中我得到错误 500,没有附加信息:
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /test/categories HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Fri, 06 Mar 2015 06:55:22 GMT
< Connection: close
<
* Closing connection 0
这是我的spring-数据-rest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
...namespaces..>
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
<jpa:repositories base-package="com.test" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jdbcDataSource"/>
<property name="packagesToScan" value="com.mammsoft.dal.hibernate" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
</beans>
这里是 class,我正在尝试为其获取数据:
@Entity
public class Category implements java.io.Serializable {
private static final long serialVersionUID = -1023381733544304297L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="name")
private String name;
@ManyToMany(fetch = FetchType.LAZY)
private Set<ProductCategory> productCategories = new HashSet<ProductCategory>(0);
@OneToMany(fetch = FetchType.LAZY)
private Set<Task> tasks = new HashSet<Task>(0);
public Category() {
}
...
//getters and setters
...
}
我是否遗漏了什么?
这是spring失败请求的调试输出:
Hibernate:
select
productcat0_.category_id as category2_2_0_,
productcat0_.product_id as product_1_5_0_,
productcat0_.category_id as category2_5_0_,
productcat0_.product_id as product_1_5_1_,
productcat0_.category_id as category2_5_1_,
productcat0_.cat_order as cat_orde3_5_1_
from
public.product_category productcat0_
where
productcat0_.category_id=?
07:51:07.337 [http-bio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.NullPointerException
07:51:07.342 [http-bio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Invoking @ExceptionHandler method: public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.AbstractRepositoryRestController.handleNPE(java.lang.NullPointerException)
我已将异常追踪到 org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler
,更具体地说是这个方法:
public Link getSelfLinkFor(Object instance) {
Assert.notNull(instance, "Domain object must not be null!");
Class<? extends Object> instanceType = instance.getClass();
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(instanceType);
if (entity == null) {
throw new IllegalArgumentException(String.format("Cannot create self link for %s! No persistent entity found!",
instanceType));
}
BeanWrapper<Object> wrapper = BeanWrapper.create(instance, null);
Object id = wrapper.getProperty(entity.getIdProperty()); //this line breaks
Link resourceLink = entityLinks.linkToSingleResource(entity.getType(), id);
return new Link(resourceLink.getHref(), Link.REL_SELF);
}
这一行抛出异常:
Object id = wrapper.getProperty(entity.getIdProperty());
bean
wrapper
的 属性 设置为正确的值(设置了 id
和 name
属性,而集合为空),但是 conversionService
等于 null
.
另外 entity
的 idProperty
为空,当 BeanWrapper
尝试访问类型时,这会导致空指针异常。
我不知道如何解决这个问题。
原来我使用了错误的 @Id
注释。我使用的是 org.springframework.data.annotation.Id
而不是 javax.persistance.Id
如果您遇到同样的问题,请检查您是否使用了正确的注释。
我遇到了同样的错误,因为我在 getter 而不是属性上使用了注释:
// WRONG
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_user", unique = true, nullable = false)
public Long getId() {
return _id;
}
// OK
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_user", unique = true, nullable = false)
private Long _id;
希望对您有所帮助。
我正在尝试设置 spring-data-rest 服务。我在 Eclipse 中使用 spring-data-rest 和 hibernate。服务工作正常(返回 HAL + JSON),只要我在 table 中没有数据,我正在尝试从中获取数据。
当我尝试从 table 获得包含数据的响应时,我在浏览器中得到空白页面,eclipse 仅显示休眠查询但未显示异常,并且仅在 curl 中我得到错误 500,没有附加信息:
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /test/categories HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Fri, 06 Mar 2015 06:55:22 GMT
< Connection: close
<
* Closing connection 0
这是我的spring-数据-rest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
...namespaces..>
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
<jpa:repositories base-package="com.test" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jdbcDataSource"/>
<property name="packagesToScan" value="com.mammsoft.dal.hibernate" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
</beans>
这里是 class,我正在尝试为其获取数据:
@Entity
public class Category implements java.io.Serializable {
private static final long serialVersionUID = -1023381733544304297L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;
@Column(name="name")
private String name;
@ManyToMany(fetch = FetchType.LAZY)
private Set<ProductCategory> productCategories = new HashSet<ProductCategory>(0);
@OneToMany(fetch = FetchType.LAZY)
private Set<Task> tasks = new HashSet<Task>(0);
public Category() {
}
...
//getters and setters
...
}
我是否遗漏了什么?
这是spring失败请求的调试输出:
Hibernate:
select
productcat0_.category_id as category2_2_0_,
productcat0_.product_id as product_1_5_0_,
productcat0_.category_id as category2_5_0_,
productcat0_.product_id as product_1_5_1_,
productcat0_.category_id as category2_5_1_,
productcat0_.cat_order as cat_orde3_5_1_
from
public.product_category productcat0_
where
productcat0_.category_id=?
07:51:07.337 [http-bio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.NullPointerException
07:51:07.342 [http-bio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Invoking @ExceptionHandler method: public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.AbstractRepositoryRestController.handleNPE(java.lang.NullPointerException)
我已将异常追踪到 org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler
,更具体地说是这个方法:
public Link getSelfLinkFor(Object instance) {
Assert.notNull(instance, "Domain object must not be null!");
Class<? extends Object> instanceType = instance.getClass();
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(instanceType);
if (entity == null) {
throw new IllegalArgumentException(String.format("Cannot create self link for %s! No persistent entity found!",
instanceType));
}
BeanWrapper<Object> wrapper = BeanWrapper.create(instance, null);
Object id = wrapper.getProperty(entity.getIdProperty()); //this line breaks
Link resourceLink = entityLinks.linkToSingleResource(entity.getType(), id);
return new Link(resourceLink.getHref(), Link.REL_SELF);
}
这一行抛出异常:
Object id = wrapper.getProperty(entity.getIdProperty());
bean
wrapper
的 属性 设置为正确的值(设置了 id
和 name
属性,而集合为空),但是 conversionService
等于 null
.
另外 entity
的 idProperty
为空,当 BeanWrapper
尝试访问类型时,这会导致空指针异常。
我不知道如何解决这个问题。
原来我使用了错误的 @Id
注释。我使用的是 org.springframework.data.annotation.Id
而不是 javax.persistance.Id
如果您遇到同样的问题,请检查您是否使用了正确的注释。
我遇到了同样的错误,因为我在 getter 而不是属性上使用了注释:
// WRONG
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_user", unique = true, nullable = false)
public Long getId() {
return _id;
}
// OK
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_user", unique = true, nullable = false)
private Long _id;
希望对您有所帮助。