西安演出公司网站建设,百度首页网址是多少,一个简单的html代码,如何编写app软件先了解一下WebService的一些相关术语吧#xff1a; WebService#xff1a;WebService是一种跨编程语言和跨操作系统平台的远程调用技术。 WSDL(web service definition language)#xff1a;WSDL是webservice定义语言, 对应.wsdl文档, 一个webservice会对应一个唯一的wsdl文… 先了解一下WebService的一些相关术语吧 WebServiceWebService是一种跨编程语言和跨操作系统平台的远程调用技术。 WSDL(web service definition language)WSDL是webservice定义语言, 对应.wsdl文档, 一个webservice会对应一个唯一的wsdl文档, 定义了客户端与服务端发送请求和响应的数据格式和过程 SOAP(simple object access protocal)SOAP是简单对象访问协议 是一种简单的、基于HTTP和XML的协议, 用于在WEB上交换结构化的数据 soap消息请求消息和响应消息 SEI(WebService EndPoint Interface)SEI是web service的终端接口就是WebService服务器端用来处理请求的接口 CXF(Celtix XFire)一个apache的用于开发webservice服务器端和客户端的框架。 本次采用的就是CXF框架来整合 首先引入相关的pom包 !-- webservice --dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion${cxf.version}/version/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-transports-http/artifactIdversion${cxf.version}/version/dependency由于版本的冲突我将spring的版本调了并且由于cglib包中的asm包和cxf框架中的asm包有冲突我做了以下更改 !-- spring版本号 --spring.version4.1.9.RELEASE/spring.versiondependencygroupIdcglib/groupIdartifactIdcglib/artifactIdversion3.2.5/versionexclusionsexclusiongroupIdorg.ow2.asm/groupIdartifactIdasm/artifactId/exclusion/exclusions/dependency2. 接着引入spring-context-webservice.xml和cxf-config.xml: ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:cxfhttp://cxf.apache.org/corexmlns:jaxwshttp://cxf.apache.org/jaxwsxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsdimport resourcecxf-config.xml/
/beans?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:jaxwshttp://cxf.apache.org/jaxwsxmlns:contexthttp://www.springframework.org/schema/contextxmlns:jaxrshttp://cxf.apache.org/jaxrsxsi:schemaLocationhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsdimport resourceclasspath*:META-INF/cxf/cxf.xml /import resourceclasspath*:META-INF/cxf/cxf-extension-soap.xml /import resourceclasspath*:META-INF/cxf/cxf-servlet.xml /!-- id 不能重复 --jaxws:endpoint idservicemanWebService implementorclack.webserviceImp.ServicemanWebServiceImp address/servicemanws / !-- jaxrs:server idrestContainer address/jaxrs:serviceBeansref beanroomService //jaxrs:serviceBeansjaxrs:providersbean classorg.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider //jaxrs:providersjaxrs:extensionMappingsentry keyjson valueapplication/json /entry keyxml valueapplication/xml //jaxrs:extensionMappings/jaxrs:server --/beans其中jaxws:endpoint idservicemanWebService implementorclack.webserviceImp.ServicemanWebServiceImp address/servicemanws / 中的设置后续会用到。 3. 更改相应的web.xml加载顺序并加入cxf配置反正是先加载spring后加载springmvc ?xml version1.0 encodingUTF-8?
web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://xmlns.jcp.org/xml/ns/javaee xsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd idWebApp_ID version3.1display-nameArchetype Created Web Application/display-namecontext-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring-context*.xml/param-value/context-param
listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerlistenerlistener-classorg.springframework.web.context.request.RequestContextListener/listener-class/listener!-- 配置SpringMVC核心调度器 --!-- 不指定具体文件的话默认为/WEB-INF/servlet name-servlet.xml --!-- load-on-startup代表启动顺序设置为大于等于0表示容器在应用启动时就加载并初始化这个servlet --!-- 推荐拦截/风格优雅 --servletservlet-nameSpringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath*:spring-mvc.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameSpringMVC/servlet-nameurl-pattern//url-pattern/servlet-mappingservletservlet-nameCXFServlet/servlet-nameservlet-classorg.apache.cxf.transport.servlet.CXFServlet/servlet-classload-on-startup1/load-on-startup/servletservlet-mappingservlet-nameCXFServlet/servlet-nameurl-pattern/ws/*/url-pattern/servlet-mapping
/web-app4. 接着添加一个工具类用于调出springmvc中加载的service注解 package clack.utils;import java.util.Enumeration;
import javax.servlet.ServletContext;import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;public class ContextUtils {public static WebApplicationContext getSpringMVCContext() {WebApplicationContext rootWac ContextLoader.getCurrentWebApplicationContext();// 获取servletContextSystem.out.println(rootWacddddd);ServletContext servletContext rootWac.getServletContext();// 获取子容器名字最后对应servlet名字//1.查看spring容器中的对象名称String[] beannames rootWac.getBeanDefinitionNames();for(String beanname:beannames){System.out.println(beanname);}System.out.println(servletContext);//2.查看servlet中容器列表EnumerationString servletnames servletContext.getAttributeNames();while(servletnames.hasMoreElements()){System.out.println(servletnames.nextElement());}WebApplicationContext springmvc WebApplicationContextUtils.getWebApplicationContext(servletContext,org.springframework.web.servlet.FrameworkServlet.CONTEXT.SpringMVC);//System.out.println(springmvceee);return springmvc;}}5. 准备工作就绪后就建立webservice接口和webservice实现类 package clack.webservice;import java.util.List;import javax.jws.WebMethod;
import javax.jws.WebService;import clack.entity.Serviceman;WebService
public interface ServicemanWebService {//使用WebMethod注解标注WebServiceI接口中的方法WebMethodListServiceman getAllServiceman() throws Exception;
}其中使用的 getAllServiceman()方法是本身的service中的方法调用。 package clack.webserviceImp;import java.util.List;import javax.jws.WebService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import clack.entity.Serviceman;
import clack.service.ServicemanService;
import clack.utils.ContextUtils;
import clack.webservice.ServicemanWebService;
//endpointInterface 编写实现接口类名 service name 网络访问的名字 对应wsdl:service namestudentws
Component(servicemanWebService)
WebService(endpointInterface clack.webservice.ServicemanWebService, serviceName servicemanws)
public class ServicemanWebServiceImp implements ServicemanWebService{Autowiredprivate ServicemanService servicemanService;public ServicemanService getServicemanService() {return servicemanService;}public void setServicemanService(ServicemanService servicemanService) {this.servicemanService servicemanService;}Overridepublic ListServiceman getAllServiceman() throws Exception {// TODO Auto-generated method stubservicemanService (ServicemanService)ContextUtils.getSpringMVCContext().getBean(servicemanService);ListServiceman servicemans servicemanService.getAllServiceman();return servicemans;}}理清其中注解的对应关系后问题不大 启动项目输入地址http://localhost:8080/StudyMaven1/ws/servicemanws?wsdl 测试 建立一个简单的maven项目并导入相关的cxf包 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdclack/groupIdartifactIdMyWebService/artifactIdversion0.0.1-SNAPSHOT/versionpropertiescxf.version2.2.3/cxf.version/propertiesdependencies!-- webservice --dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxws/artifactIdversion${cxf.version}/version/dependencydependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-transports-http/artifactIdversion${cxf.version}/version/dependency/dependenciesbuilddefaultGoalcompile/defaultGoalfinalNameMyWebService/finalNamepluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.1/versionconfigurationsource1.8/sourcetarget1.8/targetencodingutf-8/encoding/configuration/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-resources-plugin/artifactIdversion2.6/versionconfigurationfailOnMissingWebXmlfalse/failOnMissingWebXml/configuration/plugin/pluginsresources!--编译之后包含xml --resourcedirectorysrc/main/java/directoryincludesinclude**/*.xml/include/includes/resourceresourcedirectorysrc/main/resources/directoryincludesinclude**/*/include/includes/resource/resources/build
/project2. 使用了一个插件生成相关的文件 apache-cxf-3.2.7 3. 将所得的文件拷入maven项目中并新建一个测试类测试是否能取得数据 目录树如下 其中WebServiceApp是测试类 package clack.webserviceimp;import java.net.URL;
import java.util.List;import javax.xml.namespace.QName;
import javax.xml.ws.Service;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import clack.webservice.Serviceman;
import clack.webservice.ServicemanWebService;/*** 通过客户端编程的方式调用Webservice服务**/
public class WebServiceApp {//1. 自定义方法public void mysoap() throws Exception {// CXF动态客户端工厂JaxWsDynamicClientFactory dcf JaxWsDynamicClientFactory.newInstance();// WSDL文档url配置()String wsdlUrl http://localhost:8080/StudyMaven1/ws/servicemanws?wsdl;Object[] objects null;try {// 获取CXF客户端Client client dcf.createClient(wsdlUrl);// 调用Web Service方法objects client.invoke(add, 1, 2);} catch (Exception e) {e.printStackTrace();}// 获取调用结果System.out.println(调用结果 objects[0]);System.out.println();try {// 获取CXF客户端Client client dcf.createClient(wsdlUrl);// 调用Web Service方法objects client.invoke(sayHello, World!);} catch (Exception e) {e.printStackTrace();}// 获取调用结果System.out.println(调用结果 objects[0]);}//第二种 client工具生成辅助类 需使用apche cxf工具 //步骤 cmd wsdl2java -encoding utf8 http://localhost:8080/StudyMavenSSM/ws/studentws?wsdl//public void clientsoap() throws Exception{// 创建WSDL的URL注意不是服务地址URL url new URL(http://localhost:8080/StudyMaven1/ws/servicemanws?wsdl);// 创建服务名称// 1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace// targetNamespacehttp://webserviceImp.gxa/)// 2.localPart - 服务视图名 (wsdl文档中服务名称例如wsdl:service namestudentws)QName qname new QName(http://webserviceImp.clack/, servicemanws);// 创建服务视图// 参数解释// 1.wsdlDocumentLocation - wsdl地址// 2.serviceName - 服务名称Service service Service.create(url, qname);// 获取服务实现类// 参数解释:serviceEndpointInterface - 服务端口(wsdl文档中服务端口的name属性例如wsdl:port// bindingtns:studentwsSoapBinding nameStudentWebServiceImpPort)ServicemanWebService studentWebService service.getPort(ServicemanWebService.class);//调用查询方法ListServiceman students studentWebService.getAllServiceman();for(Serviceman serviceman:students){System.out.println(serviceman.getSname());}}public static void main(String[] args) throws Exception {new WebServiceApp().clientsoap();}}4. 最后运行如下成功调用 至此一个简单的webservice就整合进去了但是其中还有很多细节无法言说学而无涯。 转载于:https://www.cnblogs.com/clack/p/10001375.html