Spring 带弹簧靴的奥林戈
我正在使用它,它适用于一个简单的JavaWeb应用程序。现在我想把它转换成弹簧靴。我删除了web.xml并向DemoServlet添加了以下两个注释Spring 带弹簧靴的奥林戈,spring,spring-boot,odata,spring-restcontroller,olingo,Spring,Spring Boot,Odata,Spring Restcontroller,Olingo,我正在使用它,它适用于一个简单的JavaWeb应用程序。现在我想把它转换成弹簧靴。我删除了web.xml并向DemoServlet添加了以下两个注释 @RestController public class DemoServlet extends DispatcherServlet { private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.g
@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);
}
}
}
元数据终结点不起作用。它返回服务文档而不是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调用之后添加以下内容:
setAttribute(“requestMapping”,“/DemoService.svc”) 可以找到olingo2和spring boot的最佳实现。我建议您看看这个存储库,它非常简单明了 您可以创建
@配置
并在其中映射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;
}
@Bean
public ServletRegistrationBean odataServlet(){
ServletRegistrationBean ODataServRegistration=新的ServletRegistrationBean(新的CXFNonSpringJaxrsServlet(),
“/DemoService.svc/*”;
Map 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”);
setInitParameters(initParameters);
回归方程;
}
@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;
}