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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17139533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:06:21  来源:igfitidea点击:

Can I use SOAP Webservices and Spring MVC together

javaweb-servicesspring

提问by Akhil K Nambiar

I have a Spring MVC project. I wrote a code something like

我有一个 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();
    }
}

Can I use Spring MVC + Webservices together? What I want is just as a controller that accepts a SOAP request and process it. The url needs to be /CallBack. I'm still as a sort of confused being a Newbie. Will something like above work. Else how do I get it going.

我可以一起使用 Spring MVC + Webservices 吗?我想要的只是一个接受 SOAP 请求并对其进行处理的控制器。网址必须是 /CallBack。作为一个新手,我仍然有点困惑。像上面这样的东西会起作用。否则我如何让它继续下去。

回答by gerrytan

I wouldn't mix Spring MVC and SOAP webservice (JAX-WS) together since they serve different purpose.

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

Better practice is to encapsulate your business operation in a service class, and expose it using both MVC controller and JAX-WS. For example:

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

HelloService

你好服务

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

HelloController has HelloService reference injected via autowiring. This is standard Spring MVC controller that invoke the service and pass the result as a model to a view (eg: hello.jsp view)

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";
    }
}

A JAX-WS endpoint also invoke the same service. The difference is the service is exposed as a SOAP web service

JAX-WS 端点也调用相同的服务。不同之处在于该服务公开为 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). Additional setup might be required.

请注意,上面的 JAX-WS 风格的 Web 服务不能保证自动适用于所有 Spring 部署,尤其是在非 Java EE 环境 (tomcat) 上部署时。可能需要额外的设置。

回答by TechTrip

Yes, there are reasons why you may want to add a web service endpoint to an existing Spring MVC app. The problem is that you will likely need to have a different path for each, which is fine.

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

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. You will need to exclude Spring-web as follows in your pom:

配置可能很棘手。首先要明白,你在添加Spring-ws依赖的时候会和Spring MVC存在依赖不匹配的情况。您需要在 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. Just make sure you set up the WebMvcConfigurerAdapterand the WsConfigurerAdapteraccordingly. They will be two separate classes, annotated individually with @Configuration @EnableWebMvcand @EnableWs @Configurationrespectively. They will have to be added to the component scan complete with your @Endpointclasses.

例如,您可以轻松地将 Spring MVC 和 Spring-WS 的 spring IO 示例混合作为测试平台。只要确保您相应地设置了WebMvcConfigurerAdapterWsConfigurerAdapter。它们将是两个独立的类,分别用@Configuration @EnableWebMvc和 进行注释@EnableWs @Configuration。它们必须与您的@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. Each path handled by each servlet.

/web/*通过导入 WSDL 并/wsep/*关闭根,使用浏览器编译、运行和测试根上下文中的 MVC 内容,以及使用 SoapUI 的 SOAP 调用。每个 servlet 处理的每个路径。