带弹簧靴的Olingo

时间:2019-11-29 19:12:49

标签: java spring-boot servlets odata olingo

我正在使用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/

元数据端点不起作用。它返回服务文档而不是xml内容。

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

有人可以解释这里发生了什么吗?

4 个答案:

答案 0 :(得分:0)

为用户使用以下代码作为处理方法。

    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);

答案 1 :(得分:0)

在handler.register调用之后添加以下内容:

req.setAttribute(“ requestMapping”,“ /DemoService.svc”);

答案 2 :(得分:0)

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

}

答案 3 :(得分:-1)

here是olingo2和spring-boot的最佳实现。我建议您看一下这个存储库,它非常简单直接。