调用接口

server:



 
package   com.test.web.resources;

import  javax.ws.rs.Consumes;
import  javax.ws.rs.POST;
import  javax.ws.rs.Path;
import  javax.ws.rs.Produces;
import  javax.ws.rs.core.MediaType;
import  javax.ws.rs.core.MultivaluedMap;

import  org.springframework.stereotype.Component;


/**
 * 测试
 *  @author  ann
 *
  */
@Path( " /test " )
@Component
public   class  Test {

    @POST
    @Path( " /formTest " )
    @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    @Consumes( " application/x-www-form-urlencoded " )
     public  String formTest(MultivaluedMap < String, String >   formParams) {
        String result   =   "" ;
        result  =   " name: "   +  formParams.getFirst( " name " )  +   "  ; nickname: " + formParams.getFirst( " nickname " );
         return  result;
    }
    @POST
    @Path("/formTest2")
    @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    @Consumes("application/x-www-form-urlencoded")
    public String formTest2(@QueryParam("uid")Long uid,MultivaluedMap<String, String>  formParams) {
        String result  = "";
        result ="uid"+ uid + " ; name:" + formParams.getFirst("name") + " ; nickname:"+formParams.getFirst("nickname");
        return result;
    }

}

client 测试例子:
 
package  com.test.test;

import  javax.ws.rs.core.MultivaluedMap;

import  com.sun.jersey.api.client.Client;
import  com.sun.jersey.api.client.WebResource;
import  com.sun.jersey.api.client.config.ClientConfig;
import  com.sun.jersey.api.client.config.DefaultClientConfig;
import  com.sun.jersey.core.util.MultivaluedMapImpl;

public   class  Test {
     private   static   final   String baseUrl = " http://localhost:8082/TestServer " ;
    
     public   void  testMultivaluedMap (){
         ClientConfig cc  =   new  DefaultClientConfig();
         Client c  =  Client.create(cc);
         MultivaluedMap  form  =   new  MultivaluedMapImpl();
         form.add( " name " , " ann zhang " );
         form.add( " nickname " , " ann  " );
         WebResource wr  =  c.resource(baseUrl  +   " /test/formTest " );
         String response  =  wr.post(String. class , form);
         //String response=wr.get(String.class); get方法
         System.out.println( " result: " + response);
        
    }

    public void testMultivaluedMap2 (){
         ClientConfig cc = new DefaultClientConfig();
         Client c = Client.create(cc);
         MultivaluedMap  form = new MultivaluedMapImpl();
         form.add("name","ann zhang");
         form.add("nickname","ann ");
         WebResource wr = c.resource(baseUrl + "/test/formTest2");
         String response = wr.queryParam("uid", "10").post(String.class, form);
         System.out.println("result:"+response);
        
    }
     public   static   void  main(String[] args){
        Test test  =   new  Test();
        test.testMultivaluedMap();
        test.testMultivaluedMap2();
    }

}

ajax传入数据需要设置参数 ‘Content-Type’ : ‘application/x-www-form-urlencoded’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值