您好,登錄后才能下訂單哦!
這篇文章主要介紹“Jersey是什么”,在日常操作中,相信很多人在Jersey是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Jersey是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
Jersey是個restfull 框架 類似于springmvc
服務(wù)端
maven
<?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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--<parent>--> <!--<artifactId>demo</artifactId>--> <!--<groupId>com.demo2</groupId>--> <!--<version>0.0.1-SNAPSHOT</version>--> <!--</parent>--> <modelVersion>4.0.0</modelVersion> <groupId>JERSEY_SERVER</groupId> <artifactId>JERSEY_SERVER</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-grizzly2</artifactId> <version>1.18</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!--指定main方法--> <mainClass>com.sean.MyResource</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
代碼
package com.sean; import java.io.IOException; import java.net.URI; import java.util.Iterator; import javax.ws.rs.Consumes; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Request; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.glassfish.grizzly.http.server.HttpServer; import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory; import com.sun.jersey.api.core.PackagesResourceConfig; import com.sun.jersey.api.core.ResourceConfig; import com.sun.jersey.spi.resource.Singleton; @Singleton @Path("service") public class MyResource { @Path("{sub_path:[a-zA-Z0-9]*}") @GET @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON}) @Produces(MediaType.TEXT_PLAIN) public String getResourceName( @PathParam("sub_path") String resourceName, @DefaultValue("Just a test!") @QueryParam("desc") String description, @Context Request request, @Context UriInfo uriInfo, @Context HttpHeaders httpHeader) { System.out.println(this.hashCode()); // 將HTTP請求打印出來 System.out.println("****** HTTP request ******"); StringBuilder strBuilder = new StringBuilder(); strBuilder.append(request.getMethod() + " "); strBuilder.append(uriInfo.getRequestUri().toString() + " "); strBuilder.append("HTTP/1.1[\\r\\n]"); System.out.println(strBuilder.toString()); MultivaluedMap<String, String> headers = httpHeader.getRequestHeaders(); Iterator<String> iterator = headers.keySet().iterator(); while(iterator.hasNext()){ String headName = iterator.next(); System.out.println(headName + ":" + headers.get(headName) + "[\\r\\n]"); } System.out.println("[\\r\\n]"); String responseStr =resourceName + "[" + description + "]"; return responseStr; } public static void main(String[] args) { URI uri = UriBuilder.fromUri("http://127.0.0.1").port(10000).build(); ResourceConfig rc = new PackagesResourceConfig("com.sean"); try { HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc); server.start(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(1000*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
客戶端
maven
<?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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--<parent>--> <!--<artifactId>demo</artifactId>--> <!--<groupId>com.demo2</groupId>--> <!--<version>0.0.1-SNAPSHOT</version>--> <!--</parent>--> <modelVersion>4.0.0</modelVersion> <groupId>JERSEY_CLIENT</groupId> <artifactId>JERSEY_CLIENT</artifactId> <version>1.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-grizzly2</artifactId> <version>1.18</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!--指定main方法--> <mainClass>com.sean.JerseyClient</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
代碼
package com.sean; import java.net.URI; import java.util.Iterator; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class JerseyClient { public static void main(String[] args) { // 要使用Jersey Client API,必須首先創(chuàng)建Client的實(shí)例 // 有以下兩種創(chuàng)建Client實(shí)例的方式 // 方式一 ClientConfig cc = new DefaultClientConfig(); cc.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 10*1000); // Client實(shí)例很消耗系統(tǒng)資源,需要重用 // 創(chuàng)建web資源,創(chuàng)建請求,接受響應(yīng)都是線程安全的 // 所以Client實(shí)例和WebResource實(shí)例可以在多個線程間安全的共享 Client client = Client.create(cc); // 方式二 // Client client = Client.create(); // client.setConnectTimeout(10*1000); // client.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 10*1000); // WebResource將會繼承Client中timeout的配置 // WebResource resource = client.resource("http://127.0.0.1:10000/service/sean?desc=description"); // // String str = resource // .accept(MediaType.TEXT_PLAIN) // .type(MediaType.TEXT_PLAIN) // .get(String.class); // System.out.println("String:" + str); URI uri = UriBuilder.fromUri("http://127.0.0.1/service/sean").port(10000) .queryParam("desc", "description").build(); WebResource resource = client.resource(uri); //header方法可用來添加HTTP頭 ClientResponse response = resource.header("auth", "123456") .accept(MediaType.TEXT_PLAIN) .type(MediaType.TEXT_PLAIN) .get(ClientResponse.class); // 將HTTP響應(yīng)打印出來 System.out.println("****** HTTP response ******"); StringBuilder strBuilder = new StringBuilder(); strBuilder.append("HTTP/1.1 "); strBuilder.append(response.getStatus() + " "); strBuilder.append(response.getStatusInfo() + "[\\r\\n]"); System.out.println(strBuilder.toString()); MultivaluedMap<String, String> headers = response.getHeaders(); Iterator<String> iterator = headers.keySet().iterator(); while(iterator.hasNext()){ String headName = iterator.next(); System.out.println(headName + ":" + headers.get(headName) + "[\\r\\n]"); } System.out.println("[\\r\\n]"); System.out.println(response.getEntity(String.class) + "[\\r\\n]"); } }
到此,關(guān)于“Jersey是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。