day63-webservice 06.在web项目中发布以类的形式发布webservice
真正用的时候都是需要部署在WEB服务器里面。
不能写主函数来发布了,需要借助于我们WEB.
4.配置web.xml,
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
这个Servlet是初始化WebService用的.
创建cxf的核心配置文件cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.cxf.web.server.HelloService">
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:outInterceptors>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint> </beans>
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- For Testing using the Spring commons processor, uncomment one of:-->
<!--
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<context:annotation-config/>
-->
<bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus" destroy-method="shutdown"/> <bean id="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"
class="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"/>
<bean id="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"
class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>
<bean id="org.apache.cxf.bus.spring.BusExtensionPostProcessor"
class="org.apache.cxf.bus.spring.BusExtensionPostProcessor"/> </beans>
myeclipse中配置spring xml自动提示
什么时候加载cxf-servlet.xml?启动的时候加载.
关联源码
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.transport.servlet; import java.io.IOException;
import java.io.InputStream; import javax.servlet.ServletConfig; import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext; public class CXFServlet extends CXFNonSpringServlet {
private boolean busCreated;
private XmlWebApplicationContext createdContext; public CXFServlet() {
} @Override
protected void loadBus(ServletConfig sc) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(sc.getServletContext());
String configLocation = sc.getInitParameter("config-location");
if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
if (configLocation != null) {
wac = createSpringContext(wac, sc, configLocation);
}
if (wac != null) {
setBus(wac.getBean("cxf", Bus.class));
} else {
busCreated = true;
setBus(BusFactory.newInstance().createBus());
}
} /**
* Try to create a spring application context from the config location.
* Will first try to resolve the location using the servlet context.
* If that does not work then the location is given as is to spring
*
* @param ctx
* @param sc
* @param configLocation
* @return
*/
private ApplicationContext createSpringContext(ApplicationContext ctx,
ServletConfig sc,
String location) {
XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
createdContext = ctx2;
ctx2.setServletConfig(sc); Resource r = ctx2.getResource(location);
try {
InputStream in = r.getInputStream();
in.close();
} catch (IOException e) {
//ignore
r = ctx2.getResource("classpath:" + location);
try {
r.getInputStream().close();
} catch (IOException e2) {
//ignore
r = null;
}
}
try {
if (r != null) {
location = r.getURL().toExternalForm();
}
} catch (IOException e) {
//ignore
}
if (ctx != null) {
ctx2.setParent(ctx);
String names[] = ctx.getBeanNamesForType(Bus.class);
if (names == null || names.length == 0) {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
} else {
ctx2.setConfigLocations(new String[] {location});
}
} else {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
createdContext = ctx2;
}
ctx2.refresh();
return ctx2;
}
public void destroyBus() {
if (busCreated) {
//if we created the Bus, we need to destroy it. Otherwise, spring will handleit.
getBus().shutdown(true);
}
if (createdContext != null) {
createdContext.close();
}
} }
loadBus(ServletConfig sc)方法什么时候被调用?Servlet什么时候被初始化?初始化应该调用一个方法(init()方法).
当你把这个里面的东西继承过来之后就不分什么外层内层的了,就是说都在一个类里面这些东西啊。
Spring的接口ApplicationContext是容器的接口.WebApplicationContextUtils是工具类,当然还有一个东西叫WebApplicationContext,它是ApplicationContext的一个子类,是Web容器对于Ioc容器的支持.
web.xml可以有一些参数的一些东西,可以有一些参数.
web.xml如果是手动指定路径那么configLocation不等于null.
if (configLocation != null) {
wac = createSpringContext(wac, sc, configLocation);
}
如果configLocation不等于null,那么它就去加载初始化init File里面的路径了.
if (configLocation == null) {
try {
InputStream is = sc.getServletContext().getResourceAsStream("/WEB-INF/cxf-servlet.xml");
if (is != null && is.available() > 0) {
is.close();
configLocation = "/WEB-INF/cxf-servlet.xml";
}
} catch (Exception ex) {
//ignore
}
}
如果configLocation等于null,就去读配置文件WEB-INF/cxf-servlet.xml.配置文件默认的位置是WEB-INF/cxf-servlet.xml.
咱们初始化的时候,在Servlet里面,那就说明在web.xml,当你交代了在这个base创建的时候,init的时候,这个配置文件cxf-servlet.xml就被读取了.读取了之后还要把服务给你发布出去.
<jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService">
<!-- 输入拦截器,打印输入的消息 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
做好了之后可以把这个服务部署在WEB服务里面.
WSDL文档是从下往上看
它会先去读WSDL文档,然后再去发送请求
这是发送的消息体:
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHello xmlns:ns2="http://server.web.rl.com/"><arg0>李四</arg0></ns2:sayHello></S:Body></S:Envelope>
这是返回的消息体:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://server.web.rl.com/"><return>李四 hello</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
在web.xml里面配置CXFServlet.CXFServlet是用作初始化用的,cxf-servlet.xml是核心的配置文件.核心的配置文件是用于为我们的这个服务类来做的这个配置,指定地址啊,指定这个服务类啊。
package com.rl.web.server; import javax.jws.WebService; @WebService //所有的webservice服务类都要加@webservice,接口的形式加在接口上 public class HelloService {
public String sayHello(String name){
return name + " hello";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!--
address:tomcat的host http://ip:port/projectName/service/后面的一端路径
implementor:指定具体的服务的类
-->
<jaxws:endpoint id="hello" address="/hello" implementor="com.rl.web.server.HelloService">
<!-- 输入拦截器,打印输入的消息 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern> <!-- 拦截这种请求 -->
</servlet-mapping>
</web-app>
package com.rl.cxf.web.client; import com.rl.web.server.HelloService;
import com.rl.web.server.HelloServiceService; public class WebClient {
public static void main(String[] args) {
HelloServiceService hss = new HelloServiceService();
HelloService hs = hss.getHelloServicePort();
String result = hs.sayHello("李四");
System.out.println(result);
}
}
day63-webservice 06.在web项目中发布以类的形式发布webservice的更多相关文章
- 获取Web项目中的控制器类以及类中Action方法
前言 在使用时需要修改命名空间.需要过滤控制器.需要过滤Action方法.结果生成表的插入语句. 代码 public ActionResult ReloadData() { #region 获取所有的 ...
- 在web项目中发布jaxws
概述 在web项目中发布基于jaxws的webservice. 参考文章:用JAX-WS在Tomcat中发布WebService 参考文章说,如果不是servlet3.0及以上,需要配置servlet ...
- 在web项目中使用cxf开发webservice,包含spring支持
本文主要介绍了,如何使用cxf内置的例子,学会开发webserivce,在web项目中使用,且包含spring支持. webserivce的开发可以使用cxf或者axis,好像还有httpclient ...
- 转 web项目中的web.xml元素解析
转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45) 阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...
- JAVA WEB项目中各种路径的获取
JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报 分类: JAVA开发(41) 1.可以在s ...
- java web项目中 获取resource路径下的文件路径
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...
- Log4j2在WEB项目中配置
最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...
- (转)关于java和web项目中的相对路径问题
原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...
- Axis2在Web项目中整合Spring
一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...
随机推荐
- Oracle存储过程给变量赋值的方法
截止到目前我发现有三种方法可以在存储过程中给变量进行赋值: 1.直接法 := 如:v_flag := 0; 2.select into 如:假设变量名为v_flag,select count( ...
- SLAM: 图像角点检测的Fast算法(时间阈值实验)
作为角点检测的一种快速方法,FastCornerDetect算法比Harris方法.SIft方法都要快一些,应用于实时性要求较高的场合,可以直接应用于SLAM的随机匹配过程.算法来源于2006年的Ed ...
- java输入输入流图解
- python tips:最内嵌套作用域规则,闭包与装饰器
在作用域与名字空间提到,python是静态作用域,变量定义的位置决定了变量作用的范围.变量沿着local,global,builtins的路径搜索,直觉上就是从里到外搜索变量,这称为最内嵌套作用域规则 ...
- mysql修改原始密码
后期修改数据库用户的密码初始密码为自动生成,我们需要情况原始密码,再修改密码,mysqldmin -u root 只能用在修改为原始密码之后使用systemctl stop mysqldvim /et ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- Python 集合 day3
集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,集合用{},里面是一个一个元素,不同于key-value形式的字典: 注意:创建一个空集合必须用 s ...
- JQuery的click,trigger触发a标签的click事件无效的问题分析
今天在做一个手机端webAPP链接下载的时候,给a标签一个下载链接,但是通过 <a id="downFile" download="" href=&quo ...
- Git 基础教程 之 创建版本库
一,选择一个合适的地方,创建空目录,下面两种方法都可 ① 手动新建 ② 使用命令: mkdir pythonwork 二,初始化,使目录变成Git可管理的仓库 执行: git i ...
- poj 2763(LCA + dfs序 +树状数组)
算是模板题了 可以用dfs序维护点到根的距离 注意些LCA的时候遇到MAXM,要-1 #include<cstdio> #include<algorithm> #include ...