当前位置: 首页 > news >正文

武威网站建设价格网站建设合同 文库

武威网站建设价格,网站建设合同 文库,百度知道合伙人答题兼职,用模板做网站一. 服务端 1. 技术栈 JDK 1.8#xff0c;Eclipse#xff0c;Maven – 开发环境SpringBoot – 基础应用程序框架wsdl4j – 为我们的服务发布 WSDLSOAP-UI – 用于测试我们的服务JAXB maven 插件 – 用于代码生成 2.创建 Spring Boot 项目 添加 Wsdl4j 依赖关系 编辑pom…一.  服务端 1. 技术栈 JDK 1.8EclipseMaven – 开发环境SpringBoot – 基础应用程序框架wsdl4j – 为我们的服务发布 WSDLSOAP-UI – 用于测试我们的服务JAXB maven 插件 – 用于代码生成 2.创建 Spring Boot 项目 添加 Wsdl4j 依赖关系 编辑pom.xml并将此依赖项添加到您的项目中。 dependencygroupIdwsdl4j/groupIdartifactIdwsdl4j/artifactId /dependency3. 创建 SOAP 域模型并生成 Java 代码 当我们遵循合同优先的方法来开发服务时我们需要首先为我们的服务创建域方法和参数。 为简单起见我们将请求和响应都保留在相同的 XSD 中但在实际的企业用例中我们将有多个 XSD 相互导入以形成最终定义。 xs:schema xmlns:xshttp://www.w3.org/2001/XMLSchema xmlns:tnshttps://www.howtodoinjava.com/xml/school targetNamespacehttps://www.howtodoinjava.com/xml/school elementFormDefaultqualifiedxs:element nameStudentDetailsRequestxs:complexTypexs:sequencexs:element namename typexs:string//xs:sequence/xs:complexType/xs:elementxs:element nameStudentDetailsResponsexs:complexTypexs:sequencexs:element nameStudent typetns:Student//xs:sequence/xs:complexType/xs:elementxs:complexType nameStudentxs:sequencexs:element namename typexs:string/xs:element namestandard typexs:int/xs:element nameaddress typexs:string//xs:sequence/xs:complexType/xs:schema 将以上文件放置在项目的resources文件夹中。 4. 将 XSD 的 JAXB maven 插件添加到 Java 对象生成 我们将使用jaxb2-maven-plugin有效地生成域类。 现在我们需要将以下 Maven 插件添加到项目的pom.xml文件的插件部分。 plugingroupIdorg.codehaus.mojo/groupIdartifactIdjaxb2-maven-plugin/artifactIdversion1.6/versionexecutionsexecutionidxjc/idgoalsgoalxjc/goal/goals/execution/executionsconfigurationschemaDirectory${project.basedir}/src/main/resources//schemaDirectoryoutputDirectory${project.basedir}/src/main/java/outputDirectoryclearOutputDirfalse/clearOutputDir/configuration /plugin 该插件使用 XJC 工具作为代码生成引擎。 XJC 将 XML 模式文件编译为完全注解的 Java 类。 现在执行上面的 maven 插件以从 XSD 生成 Java 代码。 5. 创建 SOAP Web 服务端点 StudentEndpoint类将处理对服务的所有传入请求并将调用委派给数据存储库的finder方法。 package com.example.howtodoinjava.springbootsoapservice;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.springframework.ws.server.endpoint.annotation.RequestPayload; import org.springframework.ws.server.endpoint.annotation.ResponsePayload; import com.howtodoinjava.xml.school.StudentDetailsRequest; import com.howtodoinjava.xml.school.StudentDetailsResponse;Endpoint public class StudentEndpoint {private static final String NAMESPACE_URI https://www.howtodoinjava.com/xml/school;private StudentRepository StudentRepository;Autowiredpublic StudentEndpoint(StudentRepository StudentRepository) {this.StudentRepository StudentRepository;}PayloadRoot(namespace NAMESPACE_URI, localPart StudentDetailsRequest)ResponsePayloadpublic StudentDetailsResponse getStudent(RequestPayload StudentDetailsRequest request) {StudentDetailsResponse response new StudentDetailsResponse();response.setStudent(StudentRepository.findStudent(request.getName()));return response;} }这里有一些关于注解的细节 Endpoint向 Spring WS 注册该类作为处理传入 SOAP 消息的潜在候选者。然后Spring WS 使用PayloadRoot根据消息的名称空间和 localPart 选择处理器方法。 请注意此注解中提到的命名空间 URL 和请求载荷根请求。RequestPayload表示传入的消息将被映射到方法的请求参数。ResponsePayload注解使 Spring WS 将返回的值映射到响应载荷。 创建数据存储库 如前所述我们将使用硬编码的数据作为此演示的后端让我们添加一个名为StudentRepository.java并带有 Spring Repository注解的类。 它只会将数据保存在HashMap中并且还会提供一种称为findStudent()的查找器方法。 package com.example.howtodoinjava.springbootsoapservice;import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import com.howtodoinjava.xml.school.Student;Component public class StudentRepository {private static final MapString, Student students new HashMap();PostConstructpublic void initData() {Student student new Student();student.setName(Sajal);student.setStandard(5);student.setAddress(Pune);students.put(student.getName(), student);student new Student();student.setName(Kajal);student.setStandard(5);student.setAddress(Chicago);students.put(student.getName(), student);student new Student();student.setName(Lokesh);student.setStandard(6);student.setAddress(Delhi);students.put(student.getName(), student);student new Student();student.setName(Sukesh);student.setStandard(7);student.setAddress(Noida);students.put(student.getName(), student);}public Student findStudent(String name) {Assert.notNull(name, The Students name must not be null);return students.get(name);} }6. 添加 SOAP Web 服务配置 Bean 创建带有Configuration注解的类以保存 bean 定义。 package com.example.howtodoinjava.springbootsoapservice;import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.ws.config.annotation.EnableWs; import org.springframework.ws.config.annotation.WsConfigurerAdapter; import org.springframework.ws.transport.http.MessageDispatcherServlet; import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; import org.springframework.xml.xsd.SimpleXsdSchema; import org.springframework.xml.xsd.XsdSchema;EnableWs Configuration public class Config extends WsConfigurerAdapter {Beanpublic ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {MessageDispatcherServlet servlet new MessageDispatcherServlet();servlet.setApplicationContext(applicationContext);servlet.setTransformWsdlLocations(true);return new ServletRegistrationBean(servlet, /service/*);}Bean(name studentDetailsWsdl)public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {DefaultWsdl11Definition wsdl11Definition new DefaultWsdl11Definition();wsdl11Definition.setPortTypeName(StudentDetailsPort);wsdl11Definition.setLocationUri(/service/student-details);wsdl11Definition.setTargetNamespace(https://www.howtodoinjava.com/xml/school);wsdl11Definition.setSchema(countriesSchema);return wsdl11Definition;}Beanpublic XsdSchema countriesSchema() {return new SimpleXsdSchema(new ClassPathResource(school.xsd));} }Config类扩展了WsConfigurerAdapter它配置了注解驱动的 Spring-WS 编程模型。 MessageDispatcherServlet – Spring-WS 使用它来处理 SOAP 请求。 我们需要向该 servlet 注入ApplicationContext以便 Spring-WS 找到其他 bean。 它还声明了请求的 URL 映射。 DefaultWsdl11Definition使用XsdSchema公开了标准的 WSDL 1.1。 Bean 名称studentDetailsWsdl将是将公开的 wsdl 名称。 它可以在 http// localhost8080 / service / studentDetailsWsdl.wsdl 下找到。 这是在 Spring 公开合约优先的 wsdl 的最简单方法。 此配置还在内部使用 WSDL 位置 servlet 转换servlet.setTransformWsdlLocations( true )。 如果我们看到导出的 WSDL则soap:address将具有localhost地址。 同样如果我们改为从分配给已部署机器的面向公众的 IP 地址访问 WSDL我们将看到该地址而不是localhost。 因此端点 URL 根据部署环境是动态的。 7. Spring Boot SOAP Web 服务演示 使用mvn clean install进行 maven 构建然后使用java -jar target\spring-boot-soap-service-0.0.1-SNAPSHOT.jar命令启动应用程序。 这将在默认端口8080中启动一台 tomcat 服务器并将在其中部署应用程序。 1现在转到http://localhost:8080/service/studentDetailsWsdl.wsdl查看 WSDL 是否正常运行。 WSDL 已生成 2一旦成功生成了 WSDL就可以使用该 WSDL 在 SOAP ui 中创建一个项目并测试该应用程序。 样品请求和响应如下。 请求 soapenv:Envelope xmlns:soapenvhttp://schemas.xmlsoap.org/soap/envelope/ xmlns:schhttps://www.howtodoinjava.com/xml/schoolsoapenv:Header/soapenv:Bodysch:StudentDetailsRequestsch:nameSajal/sch:name/sch:StudentDetailsRequest/soapenv:Body /soapenv:Envelope响应 SOAP-ENV:Envelope xmlns:SOAP-ENVhttp://schemas.xmlsoap.org/soap/envelope/SOAP-ENV:Header/SOAP-ENV:Bodyns2:StudentDetailsResponse xmlns:ns2https://www.howtodoinjava.com/xml/schoolns2:Studentns2:nameSajal/ns2:namens2:standard5/ns2:standardns2:addressPune/ns2:address/ns2:Student/ns2:StudentDetailsResponse/SOAP-ENV:Body /SOAP-ENV:Envelope SOAP UI 示例 二. 客户端 在运行此示例之前我们需要准备好一个 SOAP 服务该服务将从该客户端代码中调用。  运行此 SOAP 服务器项目后将从http://localhost:8080/service/studentDetailsWsdl.wsdl获取 WSDL。 将 WSDL 下载为studentDetailsWsdl.wsdl稍后将其放置在客户端项目的resources/wsdl文件夹中该文件夹将在下一步创建以生成客户端代理代码。 1. Spring Boot Soap 客户端的技术栈 JDK 1.8EclipseMaven – 开发环境SpringBoot – 基础应用程序框架maven-jaxb2-plugin插件 – 用于生成 JAXB 存根SpringBoot CommandLineRunner – 测试客户端代码 2. 使用WebServiceTemplate创建 Spring 客户端 2.1 创建启动项目 仅从具有Web Services依赖关系的 SPRING 初始化器站点创建一个 spring boot 项目。 选择依赖项并提供适当的 Maven GAV 坐标后以压缩格式下载项目。 解压缩然后将 eclipse 中的项目导入为 maven 项目。 Spring boot 项目生成 2.2 生成 SOAP 域类 现在使用maven-jaxb2-plugin maven 插件生成 JAXB 注解的存根类。 为此将此 maven 插件添加到项目的pom.xml中。 pom.xml plugingroupIdorg.jvnet.jaxb2.maven2/groupIdartifactIdmaven-jaxb2-plugin/artifactIdversion0.13.2/versionexecutionsexecutiongoalsgoalgenerate/goal/goals/execution/executionsconfigurationgeneratePackagecom.example.howtodoinjava.schemas.school/generatePackagegenerateDirectory${project.basedir}/src/main/java/generateDirectoryschemaDirectory${project.basedir}/src/main/resources/wsdl/schemaDirectoryschemaIncludesinclude*.wsdl/include/schemaIncludes/configuration /plugin 此插件将在项目的src目录的com.example.howtodoinjava.springbootsoapclient包中生成类并且此插件将检查类的生成时间戳以便仅在WSDL中发生任何更改时才生成这些类。 2.3 使用WebServiceTemplate创建 SOAP 客户端 创建一个名为SOAPConnector.java的类该类将充当对 Web 服务的所有请求的通用 Web 服务客户端。 SOAPConnector.java package com.example.howtodoinjava.springbootsoapclient;import org.springframework.ws.client.core.support.WebServiceGatewaySupport;public class SOAPConnector extends WebServiceGatewaySupport {public Object callWebService(String url, Object request){return getWebServiceTemplate().marshalSendAndReceive(url, request);} } SOAPConnector类是对WebServiceGatewaySupport的扩展它基本上是通过getWebServiceTemplate()方法提供的WebServiceTemplate内部实现注入一个接口。我们将使用此WebServiceTemplate来调用 SOAP 服务。该类还期望注入一个名为Marshaller和Unmarshaller的 spring bean它们将由配置类提供我们将在下面看到。 2.4 Spring bean 配置 现在我们需要创建一个用Configuration注解的配置类该类将具有SOAPConnector所需的必需的 bean 定义以使其正常工作。 Config.java package com.example.howtodoinjava.springbootsoapclient;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller;Configuration public class Config {Beanpublic Jaxb2Marshaller marshaller() {Jaxb2Marshaller marshaller new Jaxb2Marshaller();// this is the package name specified in the generatePackage specified in// pom.xmlmarshaller.setContextPath(com.example.howtodoinjava.schemas.school);return marshaller;}Beanpublic SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {SOAPConnector client new SOAPConnector();client.setDefaultUri(http://localhost:8080/service/student-details);client.setMarshaller(marshaller);client.setUnmarshaller(marshaller);return client;} } WebServiceGatewaySupport需要Marshaller和Unmarshaller它们是Jaxb2Marshaller类的实例。它使用com.example.howtodoinjava.schemas.school作为 JAXB 类的基本包。 它将使用此包创建 JAXB 上下文。我们将使用此Jaxb2Marshaller bean 作为SOAPConnector bean 的Marshaller/Unmarshaller。 2.5 使用CommandLineRunner测试 为简单起见我们将创建一个 Spring Boot 命令行运行程序该加载程序将加载 spring 上下文并调用处理器方法并将命令行参数传递给该方法。 实时地我们需要用一些其他代码替换此命令行运行程序这些代码将更适合企业。 我们需要在SpringBootApplication类中添加此命令行运行器 bean如下。 SpringBootSoapClientApplication.java package com.example.howtodoinjava.springbootsoapclient;import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import com.example.howtodoinjava.schemas.school.StudentDetailsRequest; import com.example.howtodoinjava.schemas.school.StudentDetailsResponse;SpringBootApplication public class SpringBootSoapClientApplication {public static void main(String[] args) {SpringApplication.run(SpringBootSoapClientApplication.class, args);}BeanCommandLineRunner lookup(SOAPConnector soapConnector) {return args - {String name Sajal;//Default Nameif(args.length0){name args[0];}StudentDetailsRequest request new StudentDetailsRequest();request.setName(name);StudentDetailsResponse response (StudentDetailsResponse) soapConnector.callWebService(http://localhost:8080/service/student-details, request);System.out.println(Got Response As below : );System.out.println(Name : response.getStudent().getName());System.out.println(Standard : response.getStudent().getStandard());System.out.println(Address : response.getStudent().getAddress());};} } 在这里我们从命令行获取搜索参数并创建StudentDetailsRequest对象并使用SOAPConnector调用 SOAP Web 服务。 2.6 一些可选配置 打开application.properties并添加以下配置 application.properties server.port 9090 logging.level.org.springframework.wsTRACE 在这里我们用server.port 9090将默认端口覆盖为9090因为您已经注意到我们的示例 SOAP 服务在默认端口8080中运行因为两个 Java 进程不能在同一端口中运行。 另外我们正在通过logging.level.org.springframework.wsTRACE为org.springframework.ws软件包启用TRACE日志记录。 这将在控制台中打印 SOAP 负载。 这就是我们使用 Spring Boot 消费 SOAP 服务所需要做的一切现在是时候进行测试了。 3. 示例 现在使用 maven 命令mvn clean install来构建应用程序。 我们可以从命令提示符下通过命令java -jar target\spring-boot-soap-client-0.0.1-SNAPSHOT.jar Lokesh调用命令行运行程序。 请注意我们在此处传递了一个命令行参数Lokesh该参数将在CommandLineRunner bean 的查找方法中使用。 如果没有传递任何名称我们将在该方法中传递一个默认名称。 调用命令行运行程序后我们应该看到 SOAP 服务输出并且响应已正确解组到 JAXB 对象StudentDetailsResponse。 同样我们可以在 TRACE 日志中看到完整的 SOAP 请求/响应如下所示。 3.1 输出 2017-10-09 23:20:45.548 TRACE 9204 --- [ main] o.s.ws.client.MessageTracing.received : Received response [SOAP-ENV:Envelope xmlns:SOAP-ENVhttp://schemas.xmlsoap.org/soap/envelope/SOAP-ENV:Header/SOAP-ENV:Bodyns2:StudentDetailsResponse xmlns:ns2https://www.howtodoinjava.com/xml/schoolns2:Studentns2:nameSajal/ns2:namens2:standard5/ns2:standardns2:addressPune/ns2:address/ns2:Student/ns2:StudentDetailsResponse/SOAP-ENV:Body/SOAP-ENV:Envelope] for request [SOAP-ENV:Envelope xmlns:SOAP-ENVhttp://schemas.xmlsoap.org/soap/envelope/SOAP-ENV:Header/SOAP-ENV:Bodyns2:StudentDetailsRequest xmlns:ns2https://www.howtodoinjava.com/xml/schoolns2:nameSajal/ns2:name/ns2:StudentDetailsRequest/SOAP-ENV:Body/SOAP-ENV:Envelope] Got Response As below : Name : Lokesh Standard : 6 Address : Delhi
http://www.zqtcl.cn/news/129015/

相关文章:

  • 河池网站开发工程师招聘网如何做品牌运营与推广
  • 做网站运营难吗零基础网站建设教程
  • 深圳蚂蚁网络网站建设wordpress电影主题
  • 网站域名收费吗搜索引擎不收录网站
  • 海兴网站建设价格wordpress替代软件
  • 做网站哪家服务器好小区物业管理系统
  • 上海推广网站公司网站建设首选
  • 网站建设行业分析报告网站建设视频教程
  • 服装网站建设图企业网站建设开题报告是什么
  • 建设外贸商城网站制作网站建设的中期目标
  • 网站定做地方门户网站带手机版
  • 佛山网站建设哪家评价高系统开发报价清单
  • 东莞道滘网站建设做h游戏视频网站
  • 江西营销网站建设公司网站建设 意义
  • 公司网站怎么自己做织梦品牌集团公司网站模板(精)
  • 西安市高陵区建设局网站产品网站做营销推广
  • 网站开发费是无形资产吗深圳网站建设简介
  • 网站开发架构mvc重庆巫山网站设计哪家专业
  • 广州高档网站建设电子商务网站建设的期中考试
  • 九江建设公司网站新网 网站空间
  • 网站开发时的闭包写法手机网站创建站点成功
  • 中山做网站联系电话可以做全景的网站
  • 南京网站开发推南京乐识网络站点推广的方法有哪些
  • 沧州企业网站深圳建筑招聘网
  • 汽车网站开发的需求分析怎样策划一个营销型网站
  • 网站建设公司彩铃网站模板是怎么制作
  • 代做毕设网站推荐一键安装微信
  • 网站建设评比标准人工智能的网站
  • 网站 提示建设中计算机网站建设和维护
  • 网站菜单分类怎么做wordpress黄页插件