webservice--cxf和spring结合
服务端:
实体:
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;
}
}
SEI:
package service; import java.util.List; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import entity.Pojo; /** * 接口 */ @WebService(serviceName="PojoService", portName="PojoPort", name="PojoPortType", targetNamespace="http//:Pojo" ) public interface WeatherInterface { public @WebResult(name="result")List<Pojo> queryWeather(@WebParam(name="cityName")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; /** * 实现类 */ public class Impl implements WeatherInterface { @Override public List<Pojo> queryWeather(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; } }
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- service --> <bean id="WeatherInterface" class="service.Impl" /> <!-- 通过jaxws:server方式来配置webservice --> <jaxws:server serviceClass="service.WeatherInterface" address="/weather"> <jaxws:serviceBean> <ref bean="WeatherInterface" /> </jaxws:serviceBean> </jaxws:server> </beans>
web配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- cxf的servlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> </web-app>
祝:
大家生活愉快,工作顺利