简体   繁体   English

我可以一起使用SOAP Web服务和Spring MVC吗

[英]Can I use SOAP Webservices and Spring MVC together

I have a Spring MVC project. 我有一个Spring MVC项目。 I wrote a code something like 我写了类似的代码

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

Can I use Spring MVC + Webservices together? 我可以一起使用Spring MVC + Webservices吗? What I want is just as a controller that accepts a SOAP request and process it. 我想要的只是一个接受SOAP请求并处理它的控制器。 The url needs to be /CallBack. 网址必须为/ CallBack。 I'm still as a sort of confused being a Newbie. 作为新手,我还是很困惑。 Will something like above work. 将像上面的工作。 Else how do I get it going. 否则我将如何进行。

I wouldn't mix Spring MVC and SOAP webservice (JAX-WS) together since they serve different purpose. 我不会将Spring MVC和SOAP Web服务(JAX-WS)混合在一起,因为它们具有不同的用途。

Better practice is to encapsulate your business operation in a service class, and expose it using both MVC controller and JAX-WS. 更好的做法是将业务操作封装在服务类中,并同时使用MVC控制器和JAX-WS公开它。 For example: 例如:

HelloService HelloService的

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

HelloController has HelloService reference injected via autowiring. HelloController具有通过自动装配注入的HelloService参考。 This is standard Spring MVC controller that invoke the service and pass the result as a model to a view (eg: hello.jsp view) 这是标准的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";
    }
}

A JAX-WS endpoint also invoke the same service. JAX-WS端点也调用相同的服务。 The difference is the service is exposed as a SOAP web service 区别在于该服务作为SOAP Web服务公开

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

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

Note that JAX-WS style web service above isn't guaranteed to automatically work on all Spring deployment, especially if deployed on non Java EE environment (tomcat). 请注意,上述JAX-WS样式的Web服务不能保证在所有Spring部署中都能自动运行,尤其是在非Java EE环境(tomcat)上部署时。 Additional setup might be required. 可能需要其他设置。

Yes, there are reasons why you may want to add a web service endpoint to an existing Spring MVC app. 是的,出于某些原因,您可能想将Web服务端点添加到现有的Spring MVC应用程序。 The problem is that you will likely need to have a different path for each, which is fine. 问题是您可能需要为每个路径设置不同的路径,这很好。

You will need two servlets, a standard dispatcher servlet for handling HTTP/MVC and a MessageDispatcherServlet for handling SOAP calls. 您将需要两个servlet,一个用于处理HTTP / MVC的标准调度程序servlet,以及一个用于处理SOAP调用的MessageDispatcherServlet。

The config can be tricky. 配置可能很棘手。 First understand that you will have a dependency mismatch with Spring MVC when you add in the Spring-ws dependencies. 首先要了解的是,添加Spring-ws依赖项时,与Spring MVC的依赖项将不匹配。 You will need to exclude Spring-web as follows in your pom: 您需要按照以下步骤在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>

Once you have taken care of that you will need to add in the two servlets, one to handle web requests through Spring MVC and one to handle SOAP. 处理完这些之后,您将需要添加两个Servlet,一个用于通过Spring MVC处理Web请求,另一个用于处理SOAP。

I'm assuming no-xml config using Spring 4, SpringBoot is possible as well. 我假设使用Spring 4进行no-xml配置,SpringBoot也是可能的。

Here is the key code you will add to your web initializer: 这是您将添加到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);

That is really all there is to it. 这就是全部。 The rest of the config is standard stuff, found in any number of examples. 其余的配置是标准的东西,可以在许多示例中找到。

For instance, you can mix the spring IO examples for Spring MVC and Spring-WS easily as a test bed. 例如,您可以轻松地将Spring MVC和Spring-WS的spring IO示例混合在一起作为测试平台。 Just make sure you set up the WebMvcConfigurerAdapter and the WsConfigurerAdapter accordingly. 只需确保相应地设置了WebMvcConfigurerAdapterWsConfigurerAdapter They will be two separate classes, annotated individually with @Configuration @EnableWebMvc and @EnableWs @Configuration respectively. 它们将是两个单独的类,分别使用@Configuration @EnableWebMvc@EnableWs @Configuration分别进行注释。 They will have to be added to the component scan complete with your @Endpoint classes. 必须将它们与@Endpoint类一起添加到组件扫描中。

Compile, run and test using a browser for the MVC stuff off the root context via /web/* and the SOAP calls using SoapUI by importing the WSDL and hitting /wsep/* off the root. 使用浏览器通过/web/*从根上下文编译,运行和测试MVC内容,并通过导入WSDL并从根目录/wsep/*使用SoapUI进行SOAP调用。 Each path handled by each servlet. 每个servlet处理的每个路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM