java实现webservice调用

原因:工作中经常涉及到与第三方接口进行交接,一般公司都用的是http接口请求,这次突然用到webservice,刚开始一看就不是芭比Q了嘛,但作为一个精通百度cv的人来说,怎么能轻易放弃哈哈哈哈哈哈,所以整理了一波请求webservice接口的方法,亲测好用!!!!

请求接口的前提:
接口提供方会给你一个接口的发布网址,地址大致格式如下所示:
http://localhost:8080/ws/?wsdl
然后及你就可以针对此去进行一系列骚操作了

请求方法:
方法1:

根据所提供的地址去生成对方接口的类,如下所示:
在这里插入图片描述
生成后的文件如下所示:
在这里插入图片描述
在这里插入图片描述
再将这些文件导入自己的项目目录中,然后就可以为所欲为的使用了

示例代码如下:

/**
     * 调用webservice服务方法1
     */
    @Test
    public void method1()
    {
        //调用 webservice 服务

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

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

        //设置接口类型
        factory.setServiceClass(TestService.class);

        //对接口生成代理对象
        TestService service=factory.create(TestService.class);

        //远程访问服务端方法
        String word = service.getWord();

        System.out.println(word);
    }

方法2:直接模拟http请求调用,这种方式在请求接口数量比较少的情况下还是比较使用的,具体代码如下:

/**
     * 调用webservice服务方法2 :模拟http请求
     */
    @Test
    public void method2() throws IOException {
        //定义请求URL
        URL url=new URL("http://localhost:8080/ws/?wsdl");

        HttpURLConnection connection=null;

        connection=(HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("content-type","text/xml;charset=utf-8");

        String soapXML= "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:getWord xmlns:ns2=\"http://service.hotriver/\"/></soap:Body></soap:Envelope>";
        connection.getOutputStream().write(soapXML.getBytes());

        if (connection.getResponseCode()==200){
            Scanner scanner=new Scanner(connection.getInputStream());

            while (scanner.hasNext()){
                System.out.println(scanner.nextLine());
            }
        }
    }

另外,我们如下在本地搭建webservice服务并进行调用呢?
方法有三个,这里就只贴出最简单的一个,有兴趣的同学可以自行百度哈!

1,首先需要引入相关jar包:

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

2,服务端写接口:

@WebService()
public interface TestService {
    public String getWord();
}

public class TestServiceImpl implements TestService {

    @Override
    public String getWord() {
        String word="hello tomorrow";
        return word;
    }
}

public class ServerApp
{
    public static void main( String[] args )
    {
        //服务启动

        JaxWsServerFactoryBean factory=new JaxWsServerFactoryBean();

        factory.setAddress("http://localhost:8080/ws/");

        factory.setServiceBean(new TestServiceImpl());

        factory.getInInterceptors().add(new LoggingInInterceptor());

        factory.getOutFaultInterceptors().add(new LoggingOutInterceptor());

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

        System.out.println("服务发布成功,发布地址为:"+factory.getAddress());
    }
}

到这里,就可以调用了!方法如上所示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值