The client requires the client package:
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18</version> </dependency>
The version corresponds to the previous version.
The Code is as follows:
Package client; import COM. sun. jersey. API. client. client; import COM. sun. jersey. API. client. clientresponse; import COM. sun. jersey. API. client. webresource; import Java. io. unsupportedencodingexception; import java.net. urlencoder;/*** created by WL on 2014/10/15. */public class restfulclient {public static void main (string ARGs []) {string url = "http: // localhost: 8080/services/Hello/"; client Client client = client. create (); try {URL + = urlencoder. encode ("hello", "UTF-8");} catch (unsupportedencodingexception e) {e. printstacktrace ();} webresource resource = client. resource (URL); clientresponse response = resource. get (clientresponse. class); string entity = response. getentity (string. class); system. out. println (entity );}}
The corresponding server method:
// @ Get @ produces (mediatype. text_plain) @ path ("{name}") Public String sayhello (@ pathparam ("name") string name) {return "hello," + name ;}
Therefore, the result is printed:
Hello, how are you?
Jersey client transmits Chinese Parameters