繁体   English   中英

带有 Spring Boot 的 Olingo

[英]Olingo with Spring Boot

我正在使用本教程,它适用于简单的 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。

现在我只能访问一个端点。 IE

http://localhost:8080/DemoService.svc/

元数据端点不工作。 它返回服务文档而不是 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");

您可以创建一个@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;

}

olingo2 和 spring-boot 的最佳实现可以在这里找到。 我建议看看这个存储库,它非常简单明了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM