jax-ws开发总结

时间:2024-11-07 22:04:51

服务端开发步骤:

1.定义SEI,即java中的接口

2.定义SEI的实现类,使用@webservice注解标记它是一个webservice服务类

3.发布服务

客户端开发步骤:使用jdk的service类,调用webservice

1.使用wsimport根据wsdl生成客户端调用代码

2.将生成的代码拷贝到工程中

服务端开发过程:

自定义实体类+注解开发

实体类:

package entity;

import java.util.Date;

/**
* 实体类
*/
public class Pojo {
//温度
private String detail;
//日期
private Date date;
//最高
private int temperature_max;
//最低
private int temperature_min;
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getTemperature_max() {
return temperature_max;
}
public void setTemperature_max(int temperature_max) {
this.temperature_max = temperature_max;
}
public int getTemperature_min() {
return temperature_min;
}
public void setTemperature_min(int temperature_min) {
this.temperature_min = temperature_min;
} }

接口1:

package service;

import java.util.List;

import entity.Pojo;

/**
* 接口,查询天气
*/
public interface WeatherInterface { public List<Pojo> queryWeather(String cityName); }

实现类:

package service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; import com.sun.org.glassfish.gmbal.ParameterNames; import entity.Pojo; /**
* 接口实现类
*/ @WebService(serviceName="PojoService",
portName="PojoPort",
name="PojoPortType",
targetNamespace="http//:Pojo"
//endpointInterface="service.WeatherInterface2"
//wsdlLocation="http://127.0.0.1:12345/weather"
) public class Impl implements WeatherInterface{ // @WebMethod(operationName="queryPojo",exclude=true)
@Override
public @WebResult(name="result")List<Pojo> queryWeather(@WebParam(name="cityName")String cityName) { List<Pojo> list = new ArrayList<Pojo>(); //日历附件
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DATE); Pojo pojo1 = new Pojo();
pojo1.setDetail("晴1");
pojo1.setDate(new Date());
pojo1.setTemperature_max(5);
pojo1.setTemperature_min(-6); Pojo pojo2 = new Pojo();
pojo2.setDetail("晴2");
calendar.set(Calendar.DATE, day+1);
pojo2.setDate(calendar.getTime());
pojo2.setTemperature_max(5);
pojo2.setTemperature_min(-6); Pojo pojo3 = new Pojo();
pojo3.setDetail("晴3");
calendar.set(Calendar.DATE, day+2);
pojo3.setDate(calendar.getTime());
pojo3.setTemperature_max(5);
pojo3.setTemperature_min(-6); list.add(pojo1);
list.add(pojo2);
list.add(pojo3); return list;
}
}

发布服务:

package service;

import javax.xml.ws.Endpoint;

/**
* 发布服务类
*
*/
public class FaBu { public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:12345/weather", new Impl());
}
}

客户端开发过程:

1.单独创建一个java工程用来存放代码,通过cmd窗口,进入wsimport的src目录

2.输入命令:wsimport -extension -s . http://127.0.0.1:12345/weather?wsdl

生成的代码如下:

jax-ws开发总结

3.访问服务类

package client;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import javax.xml.namespace.QName;
import javax.xml.ws.Service; import service.Impl;
import service.ImplService;
import service.Pojo; /**
* 客户端访问服务类
*
*/
public class Client { public static void main(String[] args) throws Exception {
//创建服务视图
URL wsdlDocumentLocation = new URL("http://127.0.0.1:12345/weather?wsdl");
QName serviceName = new QName("http://service/", "ImplService");
Service service = Service.create(wsdlDocumentLocation, serviceName);
//从服务视图中得到portType对象 ,接口类型
Impl port = service.getPort(Impl.class);
//调用portType方法
List<Pojo> queryWeather = port.queryWeather("郑州");
//解析
for (Pojo pojo : queryWeather) {
System.out.println(pojo.getDetail());
Date date = pojo.getDate().toGregorianCalendar().getTime();
System.out.println(new SimpleDateFormat("yyMMdd").format(date));
System.out.println(pojo.getTemperatureMax());
System.out.println(pojo.getTemperatureMin()); } }
}