springspring-bootodataspring-restcontrollerolingo

Olingo with Spring Boot


I am using this tutorial and it works for a simple java web application. Now I want to convert it to Spring Boot. I remove the web.xml and add the following two annotations to 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);
        }
    }
}

I also change the HTTPServlet to DispatcherServlet.

Now I am only able to access one end point. i.e.

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

The metadata end point is not working. It returns the service document instead of xml content.

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

Can somebody explain what is going on here?


Solution

  • You can create a @Configuration and Map your servlet in it like the following

    @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;
    
    }