javascript
jax-ws和jax-rs_带有JAX-WS和Spring的Web服务应用程序
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 --> <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.1.RELEASE</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.1.RELEASE</version> </dependency><!-- JAX-WS --> <dependency><groupId>org.jvnet.jax-ws-commons.spring</groupId><artifactId>jaxws-spring</artifactId><version>1.9</version> </dependency><dependency><groupId>com.sun.xml.ws</groupId><artifactId>jaxws-rt</artifactId><version>2.2.8</version> </dependency>web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>SOAPWebServiceExample</display-name><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>customer</servlet-name><servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>customer</servlet-name><url-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 version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core"xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"xsi:schemaLocation="http://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.xsd"><bean id="customerService" class="com.jcombat.services.customers.CustomerServiceImpl"></bean><bean id="customerEndpoint" class="com.jcombat.ws.CustomerEndpoint"><property name="service" ref="customerService" /></bean><wss:binding url="/customer"><wss:service><ws: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服務方法,則需要使用@WebMethod(exclude = true)注釋該方法,如上面的代碼片段所示。 同樣,如果我們想將Web服務方法命名為與類中指定的實際方法名稱不同的名稱( getCustomerById() ),則需要在@WebMethod批注中添加operationName屬性。
- 如果在設置項目時遇到任何依賴關系問題,可以參考此鏈接 。
3.運行應用程序
- http:// localhost:8080 / SOAPWebServiceExample / customer?wsdl
一旦點擊了上面的URL,就可以看到顯示的WSDL內容,如下面的快照所示。
我們還可以使用SOAP UI測試端點。 使用與上述相同的WSDL位置創建新的SOAP項目。
4.下載源代碼
- 下載源代碼
翻譯自: https://www.javacodegeeks.com/2016/02/web-service-application-jax-ws-spring.html
jax-ws和jax-rs
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的jax-ws和jax-rs_带有JAX-WS和Spring的Web服务应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: al是什么材料 al是哪种材料
- 下一篇: 腾势 N7 新版车型将搭载新一代英伟达