zoukankan      html  css  js  c++  java
  • webservice整合spring

    接口HelloWorld需要添加webservice注解

    package com.cs.webservice;
    
    import java.util.List;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    @WebService
    public interface HelloWorld {
    
        String sayHi(@WebParam(name="text")String text);
        String sayHiToUser(User user);
        String[] SayHiToUserList(List<User> userList);
    }
    package com.cs.webservice;
    
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    @WebService(serviceName="boy")
    public class HelloWorldImpl implements HelloWorld {
    
        Map<Integer, User> users = new LinkedHashMap<Integer, User>();
    
        public String sayHi(String text) {
            return "Hello " + text;
        }
    
        public String sayHiToUser(User user) {
            users.put(users.size() + 1, user);
            return "Hello " + user.getName();
        }
    
        public String[] SayHiToUserList(List<User> userList) {
            String[] result = new String[userList.size()];
            int i = 0;
            for (User u : userList) {
                result[i] = "Hello " + u.getName();
                i++;
            }
            return result;
        }
    
    }

    接口实现类需要添加webservice注解。

    package com.cs.webservice;
    
    public class User {
    
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }

    实体类

    package com.cs.webservice;
    
    import javax.xml.ws.Endpoint;
    
    public class webServiceApp {
          public static void main(String[] args) {
              System.out.println("web service start");
              HelloWorldImpl implementor= new HelloWorldImpl();
              String address="http://localhost:8080/CXFWebServiceDemo"; 
          Endpoint.publish(address, implementor); System.out.println(
    "web service started"); } }

    服务端代码

     访问http://localhost:8000/helloWorld?wsdl,如果出现了wsdl文档就证明发布项目成功。

    package com.cs.webservice;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class HelloWorldClient {
        public static void main(String[] args) {
            
            //集成spring和cxf
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    //        HelloWorld client1 = (HelloWorld) context.getBean("client1");
    //        User user1 = new User();
    //        user1.setName("Tony");
    //        user1.setAge(20);
    //        User user2 = new User();
    //        user2.setName("freeman");
    //        user2.setAge(50);
    //        List<User> userList = new ArrayList<User>();
    //        userList.add(user1);
    //        userList.add(user2);
    //        // String[] res = client1.SayHiToUserList(userList);
    //        // System.out.println(res[0]);
    //        // System.out.println(res[1]);
    //        String sayHi = client1.sayHi("good");
    //        System.out.println(sayHi);
            
             HelloWorld2 client2 = (HelloWorld2) context.getBean("client2");
             User user3 = new User();
             user3.setName("Jerry");
             user3.setAge(20);
             String user = client2.sayHiToUser(user3);
             System.out.println(user);
            
            
            //没有集成
    //        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
    //        svr.setServiceClass(HelloWorld.class);
    //        svr.setAddress("http://localhost:8080/CXFWebServiceDemo"); 
    //     HelloWorld hw = (HelloWorld) svr.create();
    //     User user = new User();
    //     user.setName("Tony");
    //     System.out.println(hw.sayHi("ffff")); } }

    客户端代码

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
        
        xsi:schemaLocation="
                           http://www.springframework.org/schema/beans
    
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
        
        
        <!-- 发布一个站点 -->
        <jaxws:endpoint id="boy" implementor="com.cs.webservice.HelloWorldImpl"
            address="/aaa" />
    
        <bean id="client1" class="com.cs.webservice.HelloWorld"
            factory-bean="clientFactory1" factory-method="create" /> <!-- 通过spring的bean属性在启动容器的时候依赖注入bean,通过工厂方法create -->
    
        <bean id="clientFactory1" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass" value="com.cs.webservice.HelloWorld" />
            <property name="address" value="http://localhost:8080/CXFWebServiceDemo/aaa" />
        </bean>
        
        <!-- 第二个站点 -->
        <jaxws:endpoint id="orange" implementor="com.cs.webservice.HelloWorldImpl2"
            address="/bbb" />
    
        <bean id="client2" class="com.cs.webservice.HelloWorld2"
            factory-bean="clientFactory2" factory-method="create" />
    
        <bean id="clientFactory2" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
            <property name="serviceClass" value="com.cs.webservice.HelloWorld2" />
            <property name="address" value="http://localhost:8080/CXFWebServiceDemo/bbb" />
        </bean> 
    </beans>

    applicationContext.xml的配置信息

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <display-name>cxf</display-name>
     <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
    
    
                    <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>
    
     
    
                  <servlet>
                         <servlet-name>CXFServlet</servlet-name>
                         <servlet-class>
                                org.apache.cxf.transport.servlet.CXFServlet
                         </servlet-class>
                         <load-on-startup>1</load-on-startup>
                   </servlet>
    
                   <servlet-mapping>
                          <servlet-name>CXFServlet</servlet-name>
                          <url-pattern>/*</url-pattern>
                   </servlet-mapping>
    </web-app>

    web.xml的配置

  • 相关阅读:
    f12 接口自动刷新页面 来不及看接口信息 前端有没有传值
    order by 分组报错 shop 有三个字段 根据author 选出最大的price
    mybatis 动态sql
    正则 只有英文或者数字 长度6位以上 数字或者英文全部一样
    sql :1 :2
    前端Json数据,后台String接收,如何解析
    Json数据格式化
    LeetCode63. 不同路径 II
    LeetCode62. 不同路径
    LeetCode746. 使用最小花费爬楼梯
  • 原文地址:https://www.cnblogs.com/hjy9420/p/4211632.html
Copyright © 2011-2022 走看看