Java中的Rest服务和客户端

时间:2017-07-24 22:21:40

标签: java rest client

我开发了以下休息服务:

@POST
@Path("getTokenUser")
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public String getTokenUser(@QueryParam("User")String 
User,@QueryParam("Password")String Password) throws Exception
{
    pToken ptoken=new pToken();
    return ptoken.getAuth_Token(User, Password).toString();    
}

客户:

try
  {
    Client cliente=Client.create();
    WebResource wresource=cliente.resource("http://Localhost:8084/API_FE/webresources/Facturacion/getTokenUser");
    String values="{\"User\":\"AbrahamV\",\"Password\":\"1234sa\"}";
    ClientResponse response = wresource.type("application/json").post(ClientResponse.class, values);
    String output = response.getEntity(String.class);System.out.println(output);
  }
  catch(Exception ex)
  {
      System.out.println("Error:"+ex.getMessage());
  }

但是当我运行客户端时,其余服务不会重现任何事情。我不知道我做得怎么样,我是新手。

Response Web Service Rest:
run:
{"Message":"Son necesarias las credenciales de acceso","Token":"","Code":"afe-e01"}

1 个答案:

答案 0 :(得分:0)

我可以解决它。

try
  {
    Client cliente=Client.create();
    WebResource wresource=cliente.resource("http://Localhost:8084/API_FE/webresources/Facturacion/getTokenUser");
    MultivaluedMap<String,String> data=new  MultivaluedHashMap();
    data.add("User","Abraham01");
    data.add("Password","14235afe");
    ClientResponse response =wresource.queryParams(data).post(ClientResponse.class);
    String output = response.getEntity(String.class);System.out.println(output);
  }
  catch(Exception ex)
  {
      System.out.println("Error:"+ex.getMessage());
  }

而是发送一个json结构,我在queryParams中发送一个MultivaluedMap。