CXF整合spring,springmvc

CXF与spring,springmvc的整合,调用网络服务

  1. 整合spring,springmvc所需jar包

          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
      <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
     <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-core</artifactId>
                <version>3.2.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-transports-http</artifactId>
                <version>3.2.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>3.2.3</version>
            </dependency>
     <!-- JSON -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.10.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.13.0</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.13.0</version>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.ws</groupId>
                <artifactId>jaxws-rt</artifactId>
                <version>2.3.0</version>
            </dependency>
         <!-- jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
    
    1. 根据网络wsdl地址生成客户端代码

    2. 编写spring,springmvc配置文件

      <!--扫描service包-->
          <context:component-scan base-package="cn.pdsu.service"/>
      <!--    作为客户端调用网络服务-->
          <jaxws:client id="translateClient"
                        serviceClass="cn.webxml.EnglishChineseSoap"
                        address="http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl">
      
          </jaxws:client>
      
      <!--设置静态资源不拦截-->
          <!--过滤资源文件-->
          <mvc:resources mapping="/js/**" location="/js/"/>
          <!--注解支持&消息转换器-->
          <mvc:annotation-driven>
              <mvc:message-converters register-defaults="true">
                  <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                      <property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
                  </bean>
              </mvc:message-converters>
          </mvc:annotation-driven>
          <context:component-scan base-package="cn.pdsu.controller"/>
      <!--视图解析器-->
      <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
      
    3. 配置web.xml

      <!--  启动spring容器,配置spring环境-->
        <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
      <!--  springmvc-->
        <servlet>
          <servlet-name>dispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
          <async-supported>true</async-supported>
        </servlet>
        <servlet-mapping>
          <servlet-name>dispatcherServlet</servlet-name>
          <url-pattern>/</url-pattern>
        </servlet-mapping>
      <!--  乱码-->
        <filter>
          <filter-name>encodingFilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
          </init-param>
        </filter>
        <filter-mapping>
          <filter-name>encodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
        </filter-mapping>
      
    4. 编写service接口

      package cn.pdsu.service;
      
      import org.springframework.stereotype.Service;
      
      public interface ITranslateService {
          String translate(String source);
      }
      
      
    5. 编写service实现类

      package cn.pdsu.service.impl;
      
      import cn.pdsu.service.ITranslateService;
      import cn.webxml.EnglishChineseSoap;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import java.util.List;
      
      @Service
      public class TranslateService implements ITranslateService {
          @Autowired
          private EnglishChineseSoap translate;
      
          @Override
          public String translate(String source) {
              List<String> strings = translate.translatorString(source).getString();
              return strings.get(strings.size()-2);
          }
      }
      
      
    6. 编写Controller类

      package cn.pdsu.controller;
      
      import cn.pdsu.service.impl.TranslateService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.ResponseBody;
      
      @Controller
      public class TranslateController {
          @Autowired
          private TranslateService service;
          @ResponseBody
          @RequestMapping("translate")
          public String translate(String source){
              System.out.println("---------------");
              return  service.translate(source);
          }
      }
      
      
    7. jsp页面,利用ajax发送请求

      <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
      <html>
      <head>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
          <script type="text/javascript">
              $(function (){
                  $("#translate").click(function (){
                      //获取要翻译的内容
                      var source=$("#source").val();
                      //异步发送请求,执行翻译
                      $.ajax({
                          url:"translate",
                          data:"source="+source,
                          success:function (data){
                              //将返回的翻译结果赋值给target
                              $("#target").val(data);
                          }
                      })
                  })
              })
          </script>
      </head>
      <body>
      <div>
          <textarea rows="10" cols="10" id="source">
          </textarea>
      </div>
      <div>
          <input type="button" value="翻译" id="translate">
      </div>
      <div>
          <textarea rows="10" cols="10" id="target">
          </textarea>
      </div>
      </body>
      </html>
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CXF是一个开源的Web服务框架,可以用于开发和部署SOAP和REST风格的Web服务。Spring MVC是一个基于Java的Web应用框架,用于开发和管理MVC(Model-View-Controller)模式的Web应用程序。 要集成CXFSpring MVC,首先需要在项目中引入相关的jar包。可以通过在项目的pom.xml文件中添加依赖来实现,例如: ``` <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.4.4</version> </dependency> ``` 然后,在Spring MVC的配置文件(通常是一个XML文件,例如application-context.xml)中,需要配置CXF的相关内容。可以添加以下配置: ``` <jaxws:server id="helloService" address="/helloservice"> <jaxws:serviceBean> <bean class="com.example.HelloServiceImpl" /> </jaxws:serviceBean> </jaxws:server> ``` 在上面的配置中,`helloService`是服务的ID,`/helloservice`是服务的访问地址,`com.example.HelloServiceImpl`是实现了Web服务接口的类。 最后,在Spring MVC的控制器中,可以使用`@WebServiceRef`注解来引用CXF的Web服务。例如: ``` @Controller @RequestMapping("/hello") public class HelloController { @WebServiceRef private HelloService helloService; @RequestMapping(method = RequestMethod.GET) public String sayHello(Model model) { String message = helloService.sayHello(); model.addAttribute("message", message); return "hello"; } } ``` 在上面的示例中,`HelloService`是通过`@WebServiceRef`注解引用的CXF的Web服务接口,可以在控制器中直接调用相关的方法。 通过以上步骤,就可以实现CXFSpring MVC的集成,从而开发和部署SOAP和REST风格的Web服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值