zoukankan      html  css  js  c++  java
  • Jersey Client传递中文参数

    客户端需要客户端的包:

            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-client</artifactId>
                <version>1.18</version>
            </dependency>

    版本和之前对应。

    代码如下:

    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.create();
            try {
                url+= URLEncoder.encode("你好啊","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);
    
        }
    }

    他对应的服务端方法:

        //带参数
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        @Path("{name}")
        public String sayHello(@PathParam("name")String name){
            return "hello,"+name;
        }

    所以,结果是打印:

    hello,你好啊

  • 相关阅读:
    Luogu3118:[USACO15JAN]Moovie Mooving
    Luogu4137:Rmq Problem/mex
    Luogu3092:[USACO13NOV]No Change
    BZOJ4321: queue2
    BZOJ4650 : [NOI2016]优秀的拆分
    webpack
    sublime eslint setup
    Sublime themes/ lint themes setup
    sublime text 3
    React
  • 原文地址:https://www.cnblogs.com/juepei/p/4025992.html
Copyright © 2011-2022 走看看