In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is Jersey". In daily operation, I believe many people have doubts about what Jersey is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is Jersey?" Next, please follow the editor to study!
Jersey is a restfull framework similar to springmvc.
Server side
Maven
4.0.0 JERSEY_SERVER JERSEY_SERVER 1.0 com.sun.jersey jersey-server 1.18 com.sun.jersey Jersey-grizzly2 1.18 org.apache.maven.plugins maven-shade-plugin 2.3 Package shade Com.sean.MyResource
Code
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 ()) / / print out the HTTP request 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 headers = httpHeader.getRequestHeaders () Iterator 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 to 1000);} catch (InterruptedException e) {e.printStackTrace () }}}
Client
Maven
4.0.0 JERSEY_CLIENT JERSEY_CLIENT 1.0 UTF-8 UTF-8 1.8 1.8 1.8 com.sun.jersey Jersey-client 1.18 com.sun.jersey jersey-grizzly2 1.18 Org.apache.maven.plugins maven-shade-plugin 2.3 package Shade Com.sean.JerseyClient
Code
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) {/ / to use JerseyClient API, you must first create an instance of Client / / there are two ways to create an instance of Client: ClientConfig cc = new DefaultClientConfig (); cc.getProperties () .put (ClientConfig.PROPERTY_CONNECT_TIMEOUT, 10cm 1000) / / Client instances consume system resources and need to reuse / / create web resources, create requests, and accept responses are all thread-safe / / so Client instances and WebResource instances can safely share Client client = Client.create (cc) among multiple threads; / / method 2 / / Client client = Client.create () / / client.setConnectTimeout (10 / 1000); / / client.getProperties () .put (ClientConfig.PROPERTY_CONNECT_TIMEOUT, 10 / 1000); / / WebResource will inherit the configuration of timeout in Client / / 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) The / / header method can be used to add the HTTP header ClientResponse response = resource.header ("auth", "123456") .accept (MediaType.TEXT_PLAIN) .type (MediaType.TEXT_PLAIN) .get (ClientResponse.class) / / print the HTTP response to 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 headers = response.getHeaders (); Iterator 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]") }} at this point, the study of "what is Jersey" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 218
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.