Tra*_*Xia 0 spring spring-data-jpa spring-data-rest
我配置了 Spring Data REST 投影,当可以找到相关数据时,它会按预期工作。但是当没有找到相关数据时,会抛出以下错误:
{
"timestamp": 1532021102433,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON document: EL1007E: Property or field 'model' cannot be found on null (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.Collections$UnmodifiableMap[\"processed\"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionResource[\"content\"]->com.sun.proxy.$Proxy122[\"model\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: EL1007E: Property or field 'model' cannot be found on null (through reference chain: org.springframework.hateoas.PagedResources[\"_embedded\"]->java.util.Collections$UnmodifiableMap[\"processed\"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionResource[\"content\"]->com.sun.proxy.$Proxy122[\"model\"])",
"path": "/processed/search/findByCompCode"
}
Run Code Online (Sandbox Code Playgroud)
我的投影界面如下所示:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.config.Projection;
import java.util.Date;
@Projection(name = "usage-detail", types = {Usage.class})
interface UsageProjection {
String getUuid();
String getProduct();
Date getReportDate();
String getVin();
@Value("#{target.vehicleInfo.make}")
String getMake();
@Value("#{target.vehicleInfo.model}")
String getModel();
@Value("#{target.vehicleInfo.modelYear}")
Integer getModelYear();
}
Run Code Online (Sandbox Code Playgroud)
“vehicleInfo”是主Usage类中的一个字段,它有一个@ManyToOne连接到另一个名为VehicleInfo的类,该类提供品牌、型号和modelYear数据点。然而,并非每个使用记录都有匹配的vehicleInfo。当未找到匹配项时,将引发错误。
我正在寻找一种为vehicleInfo提供默认值的方法。根据 Spring 文档,我可以这样做:
@Value("#{target.vehicleInfo.make} ?: 'not available' ")
String getMake();
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用。:( 我仍然遇到与上面引用的相同的错误。
该错误表明vehicleInfo其本身为空,因此您需要使用安全导航运算符。此外,括号需要放在整个表达式周围,以便 SpEL 能够计算整个表达式。
@Value(\xe2\x80\x9c#{target.vehicleInfo?.make ?: \xe2\x80\x98not available\xe2\x80\x99}\xe2\x80\x9c)\nString getMake();\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2718 次 |
| 最近记录: |