我可以一起使用SOAP Webservices和Spring MVC吗?

我有一个Spring MVC项目。 我写了类似的代码

@Controller @RequestMapping("CallBack") @WebService(name = "NotificationToCP", targetNamespace = "http://SubscriptionEngine.ibm.com") public class CallbackController { @RequestMapping("") @ResponseBody @WebMethod(action = "notificationToCP") @RequestWrapper(localName = "notificationToCP", targetNamespace = "http://SubscriptionEngine.ibm.com", className = "in.co.mobiz.airtelVAS.model.NotificationToCP_Type") @ResponseWrapper(localName = "notificationToCPResponse", targetNamespace = "http://SubscriptionEngine.ibm.com", className = "in.co.mobiz.airtelVAS.model.NotificationToCPResponse") public NotificationToCPResponse index( @WebParam(name = "notificationRespDTO", targetNamespace = "") CPNotificationRespDTO notificationRespDTO) { return new NotificationToCPResponse(); } } 

我可以一起使用Spring MVC + Webservices吗? 我想要的只是作为一个接受SOAP请求并处理它的控制器。 url需要/ CallBack。 我仍然像一个新手一样迷茫。 上面的东西会起作用吗 另外,我如何实现目标。

我不会将Spring MVC和SOAP webservice(JAX-WS)混合在一起,因为它们用于不同的目的。

更好的做法是将业务操作封装在服务类中,并使用MVC控制器和JAX-WS公开它。 例如:

HelloService的

 @Service public class HelloService { public String sayHello() { return "hello world"; } } 

HelloController具有通过自动assembly注入的HelloService引用。 这是标准的Spring MVC控制器,它调用服务并将结果作为模型传递给视图(例如:hello.jsp视图)

 @Controller @RequestMapping("/hello") public class HelloController { @Autowired private HelloService helloService; @RequestMapping(method = RequestMethod.GET) public String get(Model model) { model.addAttribute("message", helloService.sayHello()); return "hello"; } } 

JAX-WS端点也调用相同的服务。 不同之处在于服务作为SOAP Web服务公开

 @WebService(serviceName="HelloService") public class HelloServiceEndpoint { @Autowired private HelloService helloService; @WebMethod public String sayHello() { return helloService.sayHello(); } } 

请注意,上述JAX-WS样式的Web服务无法保证自动在所有Spring部署上工作,尤其是在非Java EE环境(tomcat)上部署时。 可能需要其他设置。

是的,您可能希望将Web服务端点添加到现有的Spring MVC应用程序中。 问题是你可能需要为每个路径设置不同的路径,这很好。

您将需要两个servlet,一个用于处理HTTP / MVC的标准调度程序servlet和一个用于处理SOAP调用的MessageDispatcherServlet。

配置可能很棘手。 首先要了解当你添加Spring-ws依赖项时,你将与Spring MVC的依赖项不匹配。 你需要在你的pom中按如下方式排除Spring-web:

  org.springframework.ws spring-ws-core 2.2.1.RELEASE   org.springframework spring-web    

一旦你处理好了,你将需要添加两个servlet,一个用于通过Spring MVC处理Web请求,另一个用于处理SOAP。

我假设使用Spring 4的no-xml配置,SpringBoot也是可能的。

以下是您将添加到Web初始化程序的关键代码:

 DispatcherServlet servlet = new DispatcherServlet(); // no explicit configuration reference here: everything is configured in the root container for simplicity servlet.setContextConfigLocation(""); /* TMT From Java EE 6 API Docs: * Registers the given servlet instance with this ServletContext under the given servletName. * The registered servlet may be further configured via the returned ServletRegistration object. */ ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", servlet); appServlet.setLoadOnStartup(1); appServlet.setAsyncSupported(true); Set mappingConflicts = appServlet.addMapping("/web/*"); MessageDispatcherServlet mds = new MessageDispatcherServlet(); mds.setTransformWsdlLocations(true); mds.setApplicationContext(context); mds.setTransformWsdlLocations(true); ServletRegistration.Dynamic mdsServlet = servletContext.addServlet("mdsServlet", mds); mdsServlet.addMapping("/wsep/*"); mdsServlet.setLoadOnStartup(2); mdsServlet.setAsyncSupported(true); 

这就是它的全部内容。 配置的其余部分是标准的东西,可以在任何数量的例子中找到。

例如,您可以轻松地将Spring MVC和Spring-WS的弹簧IO示例混合为测试平台。 只需确保相应地设置WebMvcConfigurerAdapterWsConfigurerAdapter 。 它们将是两个单独的类,分别使用@Configuration @EnableWebMvc@EnableWs @Configuration注释。 必须使用@Endpoint类将它们添加到组件扫描中。

使用浏览器通过/web/*在根上下文中使用浏览器编译,运行和测试,并使用SoapUI通过导入WSDL并从根目录下执行/wsep/*来调用SOAP。 每个servlet处理的每个路径。