新建一个WebService项目
目录结构
第一种方式
service
package example.service;
import example.entity.WeatherDO;
public interface WeatherService {
String queryCity(String cityName);
String queryCityWeather(String cityName, String weather);
WeatherDO queryCityWeather2(WeatherDO weatherDO);
}
impl
package example.service.impl;
import example.entity.WeatherDO;
import example.service.WeatherService;
import javax.jws.WebService;
@WebService
public class WeatherServiceImpl implements WeatherService {
@Override
public String queryCity(String cityName) {
String returnStr = "查询 " + cityName;
System.out.println(returnStr);
return returnStr;
}
@Override
public String queryCityWeather(String cityName, String weather) {
String returnStr = "查询 " + cityName + ",天气" + weather;
System.out.println(returnStr);
return returnStr;
}
@Override
public WeatherDO queryCityWeather2(WeatherDO weatherDO) {
String returnStr = "查询 " + weatherDO.getCityName() + ",天气" + weatherDO.getWeather();
System.out.println(returnStr);
return weatherDO;
}
}
entity
package example.entity;
public class WeatherDO {
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
private String cityName;
private String weather;
}
WeatherMain
package example;
import example.service.impl.WeatherServiceImpl;
import javax.xml.ws.Endpoint;
public class WeatherMain {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/weatherService", new WeatherServiceImpl());
System.out.println("发布成功");
}
}
直接右键启动WeatherMain
浏览器访问
http://localhost:9999/weatherService?wsdl
soapUI测试
第二种方式
配置tomcat
访问 http://localhost:8080/test_webservice/
说明项目启动成功
生成代码
注意这里要修改成 tomcat配置的访问ip
修改后的 http://localhost:8080/test_webservice/services/HelloWorld
点击OK生成
添加必要的jar包
设置一下
访问http://localhost:8080/webservice/services/HelloWorld?wsdl
访问http://localhost:8080/webservice/services/HelloWorld
访问 http://localhost:8080/webservice/services