Java 我可以同时使用SOAP Web服务和Spring MVC吗

Java 我可以同时使用SOAP Web服务和Spring MVC吗,java,web-services,spring,Java,Web Services,Spring,我有一个SpringMVC项目。我写了一个类似这样的代码 @Controller @RequestMapping("CallBack") @WebService(name = "NotificationToCP", targetNamespace = "http://SubscriptionEngine.ibm.com") public class CallbackController { @RequestMapping("") @ResponseBody @WebMe

我有一个SpringMVC项目。我写了一个类似这样的代码

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

我可以一起使用SpringMVC+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通过自动连线注入HelloService引用。这是标准的SpringMVC控制器,用于调用服务并将结果作为模型传递给视图(例如:hello.jsp视图)

JAX-WS端点也会调用相同的服务。区别在于该服务是作为soapweb服务公开的

@WebService(serviceName="HelloService")
public class HelloServiceEndpoint {
    @Autowired private HelloService helloService;

    @WebMethod
    public String sayHello() {
        return helloService.sayHello();
    }
}

注意,上述JAX-WS风格的web服务不能保证在所有Spring部署上自动工作,特别是在非JavaEE环境(tomcat)上部署时。可能需要额外的设置。

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

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

配置可能很棘手。首先要了解,当您添加SpringWS依赖项时,您将与SpringMVC存在依赖项不匹配。您需要在pom中排除Spring web,如下所示:

<dependency>
    <groupId>org.springframework.ws</groupId>
    <artifactId>spring-ws-core</artifactId>
    <version>2.2.1.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </exclusion>
    </exclusions>
</dependency>

org.springframework.ws
SpringWS核心
2.2.1.发布
org.springframework
弹簧网
一旦处理完这些,您将需要添加两个servlet,一个用于通过SpringMVC处理web请求,另一个用于处理SOAP

我假设没有使用Spring4的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<String> 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);
DispatcherServlet servlet=new DispatcherServlet();
//这里没有明确的配置参考:为了简单起见,所有配置都在根容器中配置
servlet.setContextConfigLocation(“”);
/*Java EE 6 API文档中的TMT:
*在给定的servletName下使用此ServletContext注册给定的servlet实例。
*可以通过返回的ServletRegistration对象进一步配置已注册的servlet。
*/
ServletRegistration.DynamicAppServlet=servletContext.addServlet(“appServlet”,servlet);
appServlet.setLoadOnStartup(1);
支持appServlet.setAsyncSupported(true);
设置mappingConflicts=appServlet.addMapping(“/web/*”);
MessageDispatcherServlet mds=new MessageDispatcherServlet();
mds.setTransformWsdlLocations(true);
mds.setApplicationContext(上下文);
mds.setTransformWsdlLocations(true);
ServletRegistration.DynamicMDSServlet=servletContext.addServlet(“mdsServlet”,mds);
mdsServlet.addMapping(“/wsep/*”);
mdsServlet.setLoadOnStartup(2);
mdsServlet.setAsyncSupported(true);
这就是它的全部。配置的其余部分是标准配置,可以在任意数量的示例中找到

例如,您可以将SpringMVC和SpringWS的SpringIO示例轻松混合作为测试平台。只需确保相应地设置了
WebMVCConfigureAdapter
WsConfigurerAdapter
。它们将是两个独立的类,分别用
@Configuration@EnableWebMvc
@EnableWs@Configuration
注释。它们必须与
@Endpoint
类一起添加到组件扫描中


通过导入WSDL并点击根目录下的
/wsep/*
,使用浏览器从根目录下编译、运行和测试MVC内容,并使用SoapUI进行SOAP调用。每个servlet处理的每个路径。

这可能会起作用。(“可能”因为我从未尝试过,但由于这些只是注释,它们不会有副作用:不认识它们的人将忽略它们。)请注意,处理SOAP请求的实例与处理Spring MVC请求的实例不同。您有任何示例程序吗?你能分享吗?你如何在你的例子中定义上下文?为每个servlet定义单独bean的最佳方法是什么?
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<String> 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);