getdate odi onf weather servle 输入 att spa
1. 下载 apache-cxf-2.7.6jar 包, 并把 lib 目录下的所有 jar 包导入项目
2. 编写测试的实体类, 示例如下:
- 1 package cn.bd.weather.entity;
- 2 3 import java.util.Date;
- 4 5 import javax.xml.bind.annotation.XmlRootElement;
- 6
- /**
- 7 *
- 8 * @author Administrator
- 9 * @XmlRootElement 表示根元素
- 10 */
- 11@XmlRootElement(name = "Weather") //name属性默认是类名
- 12 public class Weather {
- 13 private String city;
- 14 private Date date;
- 15 private int min;
- 16 private int max;
- 17 private int average;
- 18 private String desc;
- 19 20 public Weather() {
- 21 super();
- 22
- }
- 23 public Weather(String city, Date date, int min, int max, int average, 24 String desc) {
- 25 super();
- 26 this.city = city;
- 27 this.date = date;
- 28 this.min = min;
- 29 this.max = max;
- 30 this.average = average;
- 31 this.desc = desc;
- 32
- }
- 33 public String getCity() {
- 34
- return city;
- 35
- }
- 36 public void setCity(String city) {
- 37 this.city = city;
- 38
- }
- 39 public Date getDate() {
- 40
- return date;
- 41
- }
- 42 public void setDate(Date date) {
- 43 this.date = date;
- 44
- }
- 45 public int getMin() {
- 46
- return min;
- 47
- }
- 48 public void setMin(int min) {
- 49 this.min = min;
- 50
- }
- 51 public int getMax() {
- 52
- return max;
- 53
- }
- 54 public void setMax(int max) {
- 55 this.max = max;
- 56
- }
- 57 public int getAverage() {
- 58
- return average;
- 59
- }
- 60 public void setAverage(int average) {
- 61 this.average = average;
- 62
- }
- 63 public String getDesc() {
- 64
- return desc;
- 65
- }
- 66 public void setDesc(String desc) {
- 67 this.desc = desc;
- 68
- }
- 69 70
- }
3. 编写服务接口, 示例如下:
- 1 package cn.bd.weather.service;
- 2
- 3 import java.util.Date;
- 4 import java.util.List;
- 5 import javax.jws.webParam;
- 6 import javax.jws.WebResult;
- 7 import javax.jws.WebService;
- 8
- 9 import cn.bd.weather.entity.Weather;
- 10
- 11@WebService(serviceName="WeatherService")//@WebService里面的targetNamespace="默认是包名"
- 12 public interface WeatherService {
- 13@WebResult(name="out")
- 14List getWeathers(@WebParam(name="city")String city,@WebParam(name="dates")List dates);
- 15 //@WebParam(name="city")表示city参数的别名
- 16
- 17}
4. 编写服务接口的实现, 示例如下:
- 1 package cn.bd.weather.service.impl;
- 2
- 3 import java.util.ArrayList;
- 4 import java.util.Date;
- 5
- 6 import java.util.List;
- 7
- 8 import javax.jws.WebMethod;
- 9 import javax.jws.WebService;
- 10
- 11 import cn.bd.weather.entity.Weather;
- 12 import cn.bd.weather.service.WeatherService;
- 13
- 14@WebService//@WebService里面的targetNamespace=""
- 15 public classWeatherServiceImplimplements WeatherService {
- 16
- 17@WebMethod//注解为web服务方法
- 18 publicList getWeathers(String city, List dates) {
- 19List list=newArrayList();
- 20 for(Date date : dates){
- 21 list.add(getWeather(city,date));
- 22 }
- 23 return list;
- 24 }
- 25
- 26@WebMethod(exclude=true)//exclude=true 表示这个web方法不对外暴露
- 27 public Weather getWeather(String city,Date date){
- 28 return newWeather(city,date,25,27,30,"晴");
- 29 }
- 30
- 31}
5. 编写 spring 容器, 示例如下:
- 1 2<beans
- 3xmlns="http://www.springframework.org/schema/beans" 4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:jaxws="http://cxf.apache.org/jaxws"
- 7xsi:schemaLocation="http://www.springframework.org/schema/beans 8http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- 9 http://cxf.apache.org/jaxws
- 10 http://cxf.apache.org/schemas/jaxws.xsd
- 11 http://cxf.apache.org/bindings/soap
- 12 http://cxf.apache.org/schemas/configuration/soap.xsd">
- 1314 <import resource="classpath:META-INF/cxf/cxf.xml"/>
- 15 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
- 16 <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
- 17
- 1819weatherService"class="cn.bd.weather.service.impl.WeatherServiceImpl">20
- 2122#weatherService" address="getWeather" publish="true">2324
- 2526 如何查看web服务是否了布成功
- 27 要在web.xml配置CXFServlet和装载spring容器
- 28 1.起动tomcat,在浏览器输入localhost:8090/CXFService/getWeather
- 29 其中getWeather 是配置Endpoint,属性address的值
- 30-->31
6.web.xml 的配置
- 1 2 3xmlns="http://java.sun.com/xml/ns/javaee" 4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- 7 8
- 9index.jsp1011
- 12 <context-param>
- 13 <param-name>contextConfigLocation</param-name>
- 14 <param-value>classpath:applicationContext.xml</param-value> <!-- 加载spring容器 -->
- 15 </context-param>
- 16 <listener>
- 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 监听spring容器 -->
- 18 </listener>
- 19
- 2021 <servlet>
- 22 <servlet-name>CXFServlet</servlet-name>
- 23 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- 24 </servlet>
- 25 <servlet-mapping>
- 26 <servlet-name>CXFServlet</servlet-name>
- 27 <url-pattern>/*</url-pattern>
- 28 </servlet-mapping>
- 29
- 30 </web-app>
用 CXF 发布 Web 服务
来源: http://www.bubuko.com/infodetail-2019202.html