3

我有一个 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。作为一个新手,我仍然有点困惑。会不会像上面那样工作。否则我该怎么办。

4

2 回答 2

6

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

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

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)

@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

@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.

于 2013-06-17T03:59:32.443 回答
0

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

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

配置可能很棘手。首先要明白,在添加 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>

处理完这些后,您将需要添加两个 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<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);

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

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

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

于 2015-08-05T04:15:51.593 回答