webservice java project records notes

A goal: to publish webservice service by JDK

1, creating an interface
2, create an implementation class WebService @ +
3, Publishing Service

 

 

 IWeatherService.java

public interface IWeatherService {

    public String Query(String cityName);
    
    public String queryother(String cityname);
}

WeatherServiceImpl.java

import javax.jws.WebService;

@WebService
public class WeatherServiceImpl implements IWeatherService {

    @Override
    public String Query(String cityName) {
        System.out.println("查询!"+cityName);
        return "121212";
    }

    @Override
    public String queryother(String cityname) {
        // TODO Auto-generated method stub
        return "1212"+cityname;
    }

}

release:

public class Main {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/weatherservice", new WeatherServiceImpl());
        System.out.println("发布成功!");

    }

}

Browser and enter:

http://localhost:8080/weatherservice?wsdl

Service instruction

Interface name: WeatherServiceImplService

 

 

 portType following class name: WeatherServiceImpl

Two methods: Query and queryother

Second, the goal: to access the service by programming

1, generate client code
command: wsimport
by cmd into the path of the current project under src,
enter the command,
wsimport -s HTTP:. // localhost: 8080 / wsdl the WeatherService?

 

Enter the command line cmd

Into the current client-side project under the src

输入:wsimport -s . http://localhost:8080/weatherservice?wsdl

 

 

 Back to the eclipse, create client refresh project

 

 

 Call webservice

main.java

import com.huk.webservice.WeatherServiceImpl;
import com.huk.webservice.WeatherServiceImplService;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1,创建服务视图
        WeatherServiceImplService webservice = new WeatherServiceImplService();
        //2,获取服务实现类
        WeatherServiceImpl port = webservice.getPort(WeatherServiceImpl.class);
        //3,调用实现方法
        String query = port.query("江西");
        System.out.println(query);
        
        String queryother = port.queryother("南昌!");
        System.out.println(queryother);
    }

}

三,目标:webservice 服务更新了,怎么办
需要从新生成client代码
先将之前的删除,在用cmd重新生成
利用命令 wsimport -s . http://localhost:8080/weatherservice?wsdl



Guess you like

Origin www.cnblogs.com/hu-kang/p/12128624.html