服务器端J2EE Webservice获取参数的空值

时间:2014-07-30 12:52:17

标签: java-ee tomcat7

我是J2ee的新手,我在这里做错了什么?我的参数值为空值。

HTTP GET网址

  

的http://:8080 / mypath中同一性= ABCD&安培; identityType = 1

代码

@GET
@Path("Request")
@Consumes({ MediaType.TEXT_PLAIN })
public Response get(@PathParam("identity") String identity,
        @PathParam("identityType") int identityType) {
    System.out.println("Identity "+identity+" IdentityType "+identityType) ;

返回

  

Identity null IdentityType 0

客户端Junit代码

    String phoneNumber = new String("abcd");
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);

    String authenticateService = "http://" + SERVER
            + "/mypath?";

    List<NameValuePair> params = new LinkedList<NameValuePair>();
    params.add(new BasicNameValuePair("identity", phoneNumber));
    params.add(new BasicNameValuePair("identityType", String
            .valueOf(1)));
    String paramString = URLEncodedUtils.format(params, "utf-8");
    authenticateService += paramString;
    URI url = UriBuilder.fromUri(authenticateService).build();
    WebResource service = client.resource(url);

    System.out.println(authenticateService);

    ->Exception Here<- String identityHash = service.type(MediaType.TEXT_PLAIN).get(
            String.class);
    assertNotNull(identityHash);

客户端println返回

  

的http://:8080 / mypath中同一性= ABCD&安培; identityType = 1

2 个答案:

答案 0 :(得分:1)

修正了此问题,将PathParam替换为QueryParam

@GET
@Path("Request")
@Consumes({ MediaType.TEXT_PLAIN })
public Response get(@QueryParam("identity") String identity,
        @QueryParam("identityType") int identityType) {
    System.out.println("Identity "+identity+" IdentityType "+identityType) ;

答案 1 :(得分:0)

要使用PathParm,您必须调整网址和方法定义

URL

  

http://:8080/service/Request/abcd/1

代码

@Path("Request/{identity}/{identityType}")
public Response get(@PathParam("identity") String identity, @PathParam("identityType") int identityType)

此致