webservice


一、service

依赖

    <!-- 要进行jaxws 服务开发 -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>

    <!-- 内置jetty web服务器  -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>3.0.1</version>
    </dependency>

    <!-- 日志实现 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.2</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
            <showWarnings>true</showWarnings>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
public class HelloServiceImpl implements HelloService {
  @Override
  public String sayHello(String name) {
    return name+"hello";
  }
}

一定要记得加注解(好像还要和client在同一包下)

@WebService
public interface HelloService {
  /**
   * 接口方法
   */

  public String sayHello(String name);
}

public class Server {
  public static void main(String[] args) {
    //发布服务工厂
    JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();

    //设置webservice服务地址
    factoryBean.setAddress("http://localhost:8080/ws/hello");

    //设置服务类
    factoryBean.setServiceBean(new HelloServiceImpl());

    //添加日志输出、输出拦截器、观察soap请求,soap响应
    factoryBean.getInInterceptors().add(new LoggingInInterceptor());
    factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

    //发布服务
    factoryBean.create();

    System.out.println("发布服务成功,端口8080");

  }

}

二、client

代码如下(示例):

  <dependencies>
    <!-- 要进行jaxws 服务开发 -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>

    <!-- 内置jetty web服务器  -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>3.0.1</version>
    </dependency>

    <!-- 日志实现 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.2</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
            <showWarnings>true</showWarnings>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
@WebService
public interface HelloService {
  public String sayHello(String name);

}

public class Client {
  public static void main(String[] args) {
    //服务接口访问地址http://localhost:8080/ws/hello

    //创建代理工厂
    JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();

    //设置远程访问服务地址
    factoryBean.setAddress("http://localhost:8080/ws/hello");

    //对接口生成代理对象
    HelloService helloService=factoryBean.create(HelloService.class);

    //代理对象
    System.out.println(helloService.getClass());

    //远程访问服务方法
    String content=helloService.sayHello("jet");
    System.out.println(content);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值