我能同时使用SOAP Webservices和Spring MVC吗?
我能同时使用SOAP Webservices和Spring MVC吗?
我有一个Spring MVC项目。我写了一段类似的代码:\n
@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(); } }
\n我可以同时使用Spring MVC和Web服务吗?我希望能够创建一个作为控制器的SOAP请求,并进行处理。URL需要是/CallBack。作为一个新手,我还有点困惑。上面的代码能起作用吗?如果不能,我该如何使其正常工作?
可以在现有的Spring MVC应用程序中添加一个Web服务端点的原因有很多。问题在于,您可能需要为每个端点设置不同的路径,这是可以接受的。您需要两个Servlet,一个用于处理HTTP/MVC,另一个用于处理SOAP调用。
配置可能会比较棘手。首先要了解的是,当您添加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的无XML配置,也可以使用SpringBoot。
下面是您将添加到Web初始化器中的关键代码:
DispatcherServlet servlet = new DispatcherServlet(); // 这里没有明确的配置引用:为了简单起见,所有配置都在根容器中配置 servlet.setContextConfigLocation(""); /* 来自Java EE 6 API文档的说明: * 使用给定的servlet名称将给定的servlet实例注册到此ServletContext中。 * 可以通过返回的ServletRegistration对象进一步配置注册的servlet。 */ ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", servlet); appServlet.setLoadOnStartup(1); appServlet.setAsyncSupported(true); SetmappingConflicts = 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的spring IO示例混合在一起作为测试平台。只需确保正确设置WebMvcConfigurerAdapter
和WsConfigurerAdapter
。它们将是两个单独的类,分别使用@EnableWebMvc
和@EnableWs
进行注释。它们必须添加到组件扫描中,包括您的@Configuration
类。
编译、运行并使用浏览器测试MVC部分,通过/web/*
访问根上的SOAP调用,可以使用SoapUI导入WSDL并通过/wsep/*
进行访问。每个Servlet处理的路径都不同。
在您的示例中,如何定义上下文?为每个Servlet定义单独的bean的最佳方法是什么?
我不会将Spring MVC和SOAP webservice(JAX-WS)混合使用,因为它们有不同的目的。
更好的做法是将业务操作封装在一个服务类中,并使用MVC控制器和JAX-WS进行公开。例如:
HelloService
public class HelloService { public String sayHello() { return "hello world"; } }
HelloController通过自动装配注入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 service
@WebService(serviceName="HelloService") public class HelloServiceEndpoint { @Autowired private HelloService helloService; public String sayHello() { return helloService.sayHello(); } }
请注意,上述JAX-WS风格的web service在所有Spring部署中都不能自动工作,特别是在非Java EE环境(如tomcat)中部署时可能需要额外的设置。