Spring 引导 Olingo
Olingo with Spring Boot
我正在使用 this tutorial,它适用于简单的 java Web 应用程序。现在我想把它转换成 Spring Boot.我删除了 web.xml 并将以下两个注释添加到 DemoServlet
@RestController
public class DemoServlet extends DispatcherServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class);
@RequestMapping("/DemoService.svc/*")
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try {
// create odata handler and configure it with CsdlEdmProvider and Processor
OData odata = OData.newInstance();
ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList<EdmxReference>());
ODataHttpHandler handler = odata.createHandler(edm);
handler.register(new DemoEntityCollectionProcessor());
// let the handler do the work
handler.process(req, resp);
} catch (RuntimeException e) {
LOG.error("Server Error occurred in ExampleServlet", e);
throw new ServletException(e);
}
}
}
我还将 HTTPServlet 更改为 DispatcherServlet。
现在我只能访问一个端点。即
http://localhost:8080/DemoService.svc/
元数据端点不工作。它 returns 服务文件而不是 xml 内容。
http://localhost:8080/DemoService.svc/$metadata
有人能解释一下这是怎么回事吗?
使用以下流程方法代码。
handler.process(new HttpServletRequestWrapper(request) {
// Spring MVC matches the whole path as the servlet path
// Olingo wants just the prefix, ie upto /odata, so that it
// can parse the rest of it as an OData path. So we need to override
// getServletPath()
@Override
public String getServletPath() {
return "/DemoService.svc";
}
}, response);
在 handler.register 调用后添加以下内容:
req.setAttribute("requestMapping", "/DemoService.svc");
可以找到 olingo2 和 spring-boot 的最佳实现 here。我建议看看这个存储库,它非常简单明了。
您可以创建一个 @Configuration
并在其中映射您的 servlet,如下所示
@Bean
public ServletRegistrationBean odataServlet() {
ServletRegistrationBean odataServRegstration = new ServletRegistrationBean(new CXFNonSpringJaxrsServlet(),
"/DemoService.svc/*");
Map<String, String> initParameters = new HashMap<>();
initParameters.put("javax.ws.rs.Application", "org.apache.olingo.odata2.core.rest.app.ODataApplication");
initParameters.put("org.apache.olingo.odata2.service.factory",
"com.metalop.code.samples.olingo.springbootolingo2sampleproject.utils.JPAServiceFactory");
odataServRegstration.setInitParameters(initParameters);
return odataServRegstration;
}
我正在使用 this tutorial,它适用于简单的 java Web 应用程序。现在我想把它转换成 Spring Boot.我删除了 web.xml 并将以下两个注释添加到 DemoServlet
@RestController
public class DemoServlet extends DispatcherServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class);
@RequestMapping("/DemoService.svc/*")
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try {
// create odata handler and configure it with CsdlEdmProvider and Processor
OData odata = OData.newInstance();
ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList<EdmxReference>());
ODataHttpHandler handler = odata.createHandler(edm);
handler.register(new DemoEntityCollectionProcessor());
// let the handler do the work
handler.process(req, resp);
} catch (RuntimeException e) {
LOG.error("Server Error occurred in ExampleServlet", e);
throw new ServletException(e);
}
}
}
我还将 HTTPServlet 更改为 DispatcherServlet。
现在我只能访问一个端点。即
http://localhost:8080/DemoService.svc/
元数据端点不工作。它 returns 服务文件而不是 xml 内容。
http://localhost:8080/DemoService.svc/$metadata
有人能解释一下这是怎么回事吗?
使用以下流程方法代码。
handler.process(new HttpServletRequestWrapper(request) {
// Spring MVC matches the whole path as the servlet path
// Olingo wants just the prefix, ie upto /odata, so that it
// can parse the rest of it as an OData path. So we need to override
// getServletPath()
@Override
public String getServletPath() {
return "/DemoService.svc";
}
}, response);
在 handler.register 调用后添加以下内容:
req.setAttribute("requestMapping", "/DemoService.svc");
可以找到 olingo2 和 spring-boot 的最佳实现 here。我建议看看这个存储库,它非常简单明了。
您可以创建一个 @Configuration
并在其中映射您的 servlet,如下所示
@Bean
public ServletRegistrationBean odataServlet() {
ServletRegistrationBean odataServRegstration = new ServletRegistrationBean(new CXFNonSpringJaxrsServlet(),
"/DemoService.svc/*");
Map<String, String> initParameters = new HashMap<>();
initParameters.put("javax.ws.rs.Application", "org.apache.olingo.odata2.core.rest.app.ODataApplication");
initParameters.put("org.apache.olingo.odata2.service.factory",
"com.metalop.code.samples.olingo.springbootolingo2sampleproject.utils.JPAServiceFactory");
odataServRegstration.setInitParameters(initParameters);
return odataServRegstration;
}