在项目中我使用的是olingo 2.0.12 jar中的java代码。
在create Entity服务调用中,
- 是否有一种方法来检查所请求的实体数据插入和
- 在数据持久化之前更改列值/附加新列值?
是否有办法添加以上内容?
在下面的代码片段,
public class A extends ODataJPADefaultProcessor{
@Override
public ODataResponse createEntity(final PostUriInfo uriParserResultView, final InputStream content,
final String requestContentType, final String contentType) throws ODataJPAModelException,
ODataJPARuntimeException, ODataNotFoundException, EdmException, EntityProviderException {
// Need to check the entity name and need to alter/add column values
}
}
是的,一种可能的方法是创建自己的CustomODataJPAProcessor
,扩展ODataJPADefaultProcessor
。
你必须通过重写
方法在JPAServiceFactory
中注册这个@Override
public ODataSingleProcessor createCustomODataProcessor(ODataJPAContext oDataJPAContext) {
return new CustomODataJPAProcessor(this.oDataJPAContext);
}
现在Olingo将使用CustomODataJPAProcessor,它可以实现以下代码来检查实体并在需要时转换它们
CustomODataJPAProcessor
示例代码
public class CustomODataJPAProcessor extends ODataJPADefaultProcessor {
Logger LOG = LoggerFactory.getLogger(this.getClass());
public CustomODataJPAProcessor(ODataJPAContext oDataJPAContext) {
super(oDataJPAContext);
}
@Override
public ODataResponse createEntity(final PostUriInfo uriParserResultView, final InputStream content,
final String requestContentType, final String contentType) throws ODataException {
ODataResponse oDataResponse = null;
oDataJPAContext.setODataContext(getContext());
InputStream forwardedInputStream = content;
try {
if (uriParserResultView.getTargetEntitySet().getName().equals("Students")) {
LOG.info("Students Entity Set Executed");
if (requestContentType.equalsIgnoreCase(ContentType.APPLICATION_JSON.toContentTypeString())) {
@SuppressWarnings("deprecation")
JsonElement elem = new JsonParser().parse(new InputStreamReader(content));
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
Student s = gson.fromJson(elem, Student.class);
// Change some values
s.setStudentID("Test" + s.getStudentID());
forwardedInputStream = new ByteArrayInputStream(gson.toJson(s).getBytes());
}
}
Object createdJpaEntity = jpaProcessor.process(uriParserResultView, forwardedInputStream,
requestContentType);
oDataResponse = responseBuilder.build(uriParserResultView, createdJpaEntity, contentType);
} catch (JsonIOException | JsonSyntaxException e) {
throw new RuntimeException(e);
} finally {
close();
}
return oDataResponse;
}
}
在夏天的
- 注册您的自定义
org.apache.olingo.odata2.service.factory
代码链接 创建自己的 - 在
JPAServiceFactory
中重写createCustomODataProcessor
以使用自定义处理器代码Link
CustomODataJPAProcessor
代码链接