Jersey是什么

這篇文章主要介紹“Jersey是什么”,在日常操作中,相信很多人在Jersey是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Jersey是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴(kuò)展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,成都創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:封陽臺成都網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷推廣解決方案、網(wǎng)站設(shè)計等建站排名服務(wù)。


Jersey是個restfull 框架 類似于springmvc

Jersey是什么

服務(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的實例
//		有以下兩種創(chuàng)建Client實例的方式

//     方式一
		ClientConfig cc = new DefaultClientConfig();
		cc.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 10*1000);
//	    Client實例很消耗系統(tǒng)資源,需要重用
//	    創(chuàng)建web資源,創(chuàng)建請求,接受響應(yīng)都是線程安全的
//	    所以Client實例和WebResource實例可以在多個線程間安全的共享
		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é)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

當(dāng)前標(biāo)題:Jersey是什么
分享路徑:http://www.muchs.cn/article12/gjghdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、用戶體驗、網(wǎng)站制作、微信公眾號、網(wǎng)站建設(shè)、App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)