【Java】CXF

Apache CXF是一个开源框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构,它允许创建高性能和可扩展的服务,可以将服务部署在 Tomcat 和基于 Spring 的轻量级容器中。

准备架包:http://cxf.apache.org/;将lib包先所有jar包复制到项目目录下

创建web项目,项目结构如下图:

HelloWorldClient.java

package cn.oly.client;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import cn.oly.server.HelloWorld;
import cn.oly.server.User;

public class HelloWorldClient {
    public static void main(String[] args) throws IOException {
        // 首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码
        JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
        jwpfb.setServiceClass(HelloWorld.class);
        jwpfb.setAddress("http://localhost:8080/webServices/helloWorld");
        HelloWorld hw = (HelloWorld) jwpfb.create();
        // System.out.println(hw.sayHi(readFile()));
        User user = new User();
        user.setName("tName");
        user.setDescription("decName");
        System.out.println(hw.sayHiToUser(user));

    }

    public static String readFile() {
        // 使用字符流实现文件的读入
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        String str = "";
        int i = 0;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    "client.txt"), "GBK"));
            while ((str = br.readLine()) != null) {
                if (str.trim() != null) {
                    sb.append(str.trim());
                }
            }
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return sb.toString();
    }
}
View Code

 HelloWorld.java

package cn.oly.server;

import javax.jws.WebParam;
import javax.jws.WebService;
import java.io.FileNotFoundException;
import java.util.List;
@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text")String text) throws FileNotFoundException;
    String sayHiToUser(User user);
    String[] SayHiToUserList(List<User> userList);
    
}
 HelloWorldImpl.java

package cn.oly.server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(endpointInterface="cn.oly.server.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();

    public String sayHi(@WebParam(name = "text") String text) {
          File file = new File("D:\\server.txt");
          PrintStream ps = null;
        try {
            ps = new PrintStream(new FileOutputStream(file));
            ps.println("测试文本内容");// 往文件里写入字符串
            if(text.trim()!=null&&text.trim()!=""){
                ps.append(text.toString());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{
            ps.close();
        }
        return "Hello";
    }

    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;
    }
}
View Code

User.java

package cn.oly.server;

import java.io.Serializable;

@SuppressWarnings("serial")
public class User implements Serializable {

    private String id;
    private String name;
    private String age;
    private String description;

    public User() {
        super();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


}
View Code

Web.java

package cn.oly.testServer;

import cn.oly.server.HelloWorldImpl;

import javax.xml.ws.Endpoint;

public class Web {
    public static void main(String[] args) {
        System.out.println("web service start");
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:8080/webServices/helloWorld";
        Endpoint.publish(address, implementor);
        System.out.println("web service started");
    }

}
View Code

applicationContext.xml

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

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint
            id="helloWorld"
            implementor="cn.oly.server.HelloWorldImpl"
            address="/helloWorld" />

    <bean id="client"
          class="cn.oly.server.HelloWorld"
          factory-bean="clientFactory"
          factory-method="create"/>

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="cn.oly.server.HelloWorld"/>
        <property name="address" value="http://localhost:8080/webServices/helloWorld"/>
    </bean>

</beans>
View Code

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>webServices</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/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>

    <!-- 字符过滤器 -->
    <filter>
        <filter-name>encoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>


    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
</web-app>
View Code

访问路径:http://localhost:8080/webServices/helloWorld?wsdl

转载于:https://www.cnblogs.com/olyx/p/6409999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值