cxf的第一个例子

本文介绍了一个简单的Java Web服务示例,包括接口定义、实现类、服务启动及客户端调用流程。通过此示例,读者可以了解如何使用Java创建并调用一个基本的WebService。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.First.java

package demo.hw.server;

import javax.jws.*;

@WebService
public interface First {
    public Person sayHi(@WebParam(name = "name")String name);
}

2.FirstImpl.java

package demo.hw.server;

import javax.jws.*;

@WebService(endpointInterface = "demo.hw.server.First",
        serviceName = "HelloWorld")
public class FirstImpl implements First {

 public Person sayHi(String name) {

          Person person = new Person();
          person.name = name;
          person.sex = "man";
         
          return person;
 }

}

3.Person.java

 

package demo.hw.server;

 

public class Person {
    public String name;
    public String sex;
}

4.Server.java

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package demo.hw.server;

import javax.xml.ws.Endpoint;

public class Server {

    protected Server() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        FirstImpl implementor = new FirstImpl();
        String address = "http://localhost:9000/helloWorld";
        Endpoint.publish(address, implementor);
        // END SNIPPET: publish
    }

    public static void main(String args[]) throws Exception {
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

5.Client.java

package demo.hw.server;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

public class Client {
 public static void main(String[] args) {
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  factory.getInInterceptors().add(new LoggingInInterceptor());
  factory.getOutInterceptors().add(new LoggingOutInterceptor());
  factory.setServiceClass(First.class);
  factory.setAddress("http://localhost:9000/helloWorld");
  First client = (First) factory.create();

  Person reply = client.sayHi("HI");
  System.out.println("Server said: " + reply);
  System.exit(0);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值