9、Web Service-IDEA-jaxrs 服務端客戶端的實現
阿新 • • 發佈:2019-03-22
需要 sum package -h esp front 補充 pil --
關於RestFul編程可以參考:https://www.cnblogs.com/wang-yaz/p/9237981.html
關於jaxrs的實現需要有restful的理解。
話不多說直接上代碼!!
1、服務端的開發
1、新建web項目、工程目錄如下圖
2、pom文件添加依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cr</groupId> <artifactId>server</artifactId> <version>1.0-SNAPSHOT</version> <name>server</name> <!-- FIXME change it to the project‘s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.0.1</version> </dependency>
<!-- 內置jetty服務器 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.0.1</version> </dependency> <!--日誌--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> <scope>test</scope> </dependency> <!--restful風格客戶端調用所需要的 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.0.1</version> </dependency> <!--對json的支持 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> </build> </project>
3、類:
User.java 註意需要和client端在同一個包裏
package com.cr.entity; import javax.xml.bind.annotation.XmlRootElement; //基於restful風格的webservice //客戶端與服務的之間通訊可以傳到xml、json //該註解指定對象序列化為xml或者json數據時根節點的名稱 //xml: <User><id></id></User> //json:效率更高 {"User":"",pwd:""} @XmlRootElement(name = "User") public class User { private String name; private String pwd; //..... }
接口 UserService.java
package com.cr.service; import com.cr.entity.User; import javax.ws.rs.*; import java.util.List; //訪問當前服務接口對應的路徑 @Path("/userService") @Produces("*/*")//支持任意類型 public interface UserService { @POST @Path("/user")//訪問當前端方法對應的路徑 @Consumes({"application/xml","application/json"})//服務器支持的請求數據格式類型 public void save(User user); @GET @Path("/user") @Consumes({"application/xml","application/json"}) @Produces("application/xml") public List<User> findUser(); }
接口的實現 UserServiceImpl.java
package com.cr.service.impl; import com.cr.entity.User; import com.cr.service.UserService; import java.util.ArrayList; import java.util.List; public class UserServiceImpl implements UserService { public void save(User user){ System.out.println("save:"+user); }
public List<User> findUser(){ List<User> uList = new ArrayList<>(); User u1 = new User("aa","qwe"); User u2 = new User("bb","asd"); uList.add(u1); uList.add(u2); System.out.println(uList); return uList; } }
發布服務
package com.cr; import com.cr.service.impl.UserServiceImpl; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; public class server { public static void main(String[] args) { //發布服務的工程 JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); //設置服務的 地址 factoryBean.setAddress("http://localhost:8001/cr/"); //設置服務類 factoryBean.setServiceBean(new UserServiceImpl()); //添加日誌輸入 factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); //發布服務 factoryBean.create(); System.out.println("發布成功..."); } }
2、客戶端的代碼
1、工程結構
2、pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cr</groupId> <artifactId>client</artifactId> <version>1.0-SNAPSHOT</version> <name>client</name> <!-- FIXME change it to the project‘s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.0.1</version> </dependency>
<!-- 內置jetty服務器 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.0.1</version> </dependency> <!--日誌--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> <scope>test</scope> </dependency> <!--restful風格客戶端調用所需要的 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.0.1</version> </dependency> <!--對json的支持 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <showWarnings>true</showWarnings> </configuration> </plugin> </plugins> </build> </project>
3、類
User.java
package com.cr.entity; import javax.xml.bind.annotation.XmlRootElement; //基於restful風格的webservice //客戶端與服務的之間通訊可以傳到xml、json //該註解指定對象序列化為xml或者json數據時根節點的名稱 //xml: <User><id></id></User> //json:效率更高 {"User":"",pwd:""} @XmlRootElement(name = "User") public class User { private String name; private String pwd; //.... }
測試:
@Test public void save(){ //遠程調用服務端 WebClient. create("http://localhost:8001/cr/userService/user"). type("application/json"). post(new User("qq","22")); }
server端的打印
---------------------------- ID: 4 Address: http://localhost:8001/cr/userService/user Encoding: ISO-8859-1 Http-Method: POST Content-Type: application/json Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[31], content-type=[application/json], Host=[localhost:8001], Pragma=[no-cache], User-Agent=[Apache CXF 3.0.1]} Payload: {"User":{"name":"qq","pwd":22}} -------------------------------------- save:com.cr.entity.User@4986f0c7 INFO - LoggingOutInterceptor - Outbound Message --------------------------- ID: 4 Response-Code: 204 Content-Type: Headers: {Date=[Mon, 18 Mar 2019 11:56:29 GMT], Content-Length=[0]} --------------------------------------
測試:
//查詢 @Test public void find(){ WebClient.create("http://localhost:8001/cr/userService/user").get(); }
ID: 5 Address: http://localhost:8001/cr/userService/user Http-Method: GET Content-Type: */* Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[*/*], Host=[localhost:8001], Pragma=[no-cache], User-Agent=[Apache CXF 3.0.1]} -------------------------------------- [com.cr.entity.User@7f4f702, com.cr.entity.User@3cb7324] INFO - LoggingOutInterceptor - Outbound Message --------------------------- ID: 5 Response-Code: 200 Content-Type: application/xml Headers: {Content-Type=[application/xml], Date=[Mon, 18 Mar 2019 11:57:38 GMT]} Payload: <?xml version="1.0" encoding="UTF-8"?><Users><User><name>aa</name><pwd>qwe</pwd></User><User><name>bb</name><pwd>asd</pwd></User></Users>
3、額外的補充
額外的補充都可以嘗試去實現!!!
9、Web Service-IDEA-jaxrs 服務端客戶端的實現