买的网站模板里面是什么,做行业网站,常见的erp系统,同城信息发布平台jax-ws和jax-rs1.简介 这是一个漫长的等待#xff0c;但是我最终要发布有关使用Spring创建第一个基于SOAP的Web服务应用程序的教程。 JAX-WS #xff08;用于XML Web服务的Java API#xff09;是用于以XML格式创建Web服务的一组API#xff0c;我们最常将其称为基于SOAP的We… jax-ws和jax-rs 1.简介 这是一个漫长的等待但是我最终要发布有关使用Spring创建第一个基于SOAP的Web服务应用程序的教程。 JAX-WS 用于XML Web服务的Java API是用于以XML格式创建Web服务的一组API我们最常将其称为基于SOAP的Web服务 希望大家都了解基本架构。 2.实施 首先让我们检查一下pom文件的配置– pom.xml !-- Spring dependencies --
dependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion4.2.1.RELEASE/version
/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion4.2.1.RELEASE/version
/dependency!-- JAX-WS --
dependencygroupIdorg.jvnet.jax-ws-commons.spring/groupIdartifactIdjaxws-spring/artifactIdversion1.9/version
/dependencydependencygroupIdcom.sun.xml.ws/groupIdartifactIdjaxws-rt/artifactIdversion2.2.8/version
/dependency web.xml ?xml version1.0 encodingISO-8859-1?
web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://java.sun.com/xml/ns/javaeexsi:schemaLocationhttp://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsdidWebApp_ID version2.5display-nameSOAPWebServiceExample/display-namelistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerservletservlet-namecustomer/servlet-nameservlet-classcom.sun.xml.ws.transport.http.servlet.WSSpringServlet/servlet-classload-on-startup1/load-on-startup/servletservlet-mappingservlet-namecustomer/servlet-nameurl-pattern/customer/url-pattern/servlet-mapping/web-app 让我们为我们的应用程序创建客户实体。 客户.java package com.jcombat.entity;public class Customer {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}
} 现在让我们创建服务接口及其对应的服务实现类。 CustomerService.java package com.jcombat.services.customers;import com.jcombat.entity.Customer;public interface CustomerService {public Customer getCustomerById(String customerId);
} CustomerServiceImpl.java package com.jcombat.services.customers;import com.jcombat.entity.Customer;public class CustomerServiceImpl implements CustomerService {public Customer getCustomerById(String customerId) {Customer customer new Customer();customer.setId(123);customer.setName(Abhimanyu);return customer;}
} 下面是applicationContext的外观。 applicationContext.xml ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:wshttp://jax-ws.dev.java.net/spring/corexmlns:wsshttp://jax-ws.dev.java.net/spring/servletxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsdhttp://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsdbean idcustomerService classcom.jcombat.services.customers.CustomerServiceImpl/beanbean idcustomerEndpoint classcom.jcombat.ws.CustomerEndpointproperty nameservice refcustomerService //beanwss:binding url/customerwss:servicews:service bean#customerEndpoint //wss:service/wss:binding/beans 请注意URL模式 / customer 绑定到了Web服务端点实现类 customerEndpoint 如上面的代码片段所示。 下面是我们的customerEndpoint bean实现类的样子。 CustomerEndpoint.java package com.jcombat.ws;import javax.jws.WebMethod;
import javax.jws.WebService;import com.jcombat.entity.Customer;
import com.jcombat.services.customers.CustomerService;WebService(serviceName customerService)
public class CustomerEndpoint {private CustomerService service;WebMethod(exclude true)public void setService(CustomerService service) {this.service service;}WebMethod(operationName getCustomer)public Customer getCustomerById(String customerId) {Customer customer service.getCustomerById(customerId);return customer;}} 请注意 WebService批注指示服务器运行时环境将该类的所有公共方法公开为Web服务方法。 如果要防止将任何方法公开为Web服务方法则需要使用WebMethodexclude true注释该方法如上面的代码片段所示。 同样如果我们想将Web服务方法命名为与类中指定的实际方法名称不同的名称 getCustomerById 则需要在WebMethod批注中添加operationName属性。 如果在设置项目时遇到任何依赖关系问题可以参考此链接 。 3.运行应用程序 http// localhost8080 / SOAPWebServiceExample / customerwsdl 一旦点击了上面的URL就可以看到显示的WSDL内容如下面的快照所示。 我们还可以使用SOAP UI测试端点。 使用与上述相同的WSDL位置创建新的SOAP项目。 4.下载源代码 下载源代码 翻译自: https://www.javacodegeeks.com/2016/02/web-service-application-jax-ws-spring.htmljax-ws和jax-rs