Java API for invoking REST services [closed]

2019-05-12 04:15发布

问题:

Could any one please suggest a better open source Java API for invoking REST services? Also wanted to know if Restlet API supports NTLM authentication.

Thanks

回答1:

It's REST - the whole point is you don't need an API per se, just HttpURLConnection. You should be able to interact with any truly RESTful service with the basic Java SDK alone. You can get fancier with Apache Commons HTTPClient - but it's not a necessity.



回答2:

Check out Restlet. It has a good client API.

Example usage:

Request request = new Request(Method.GET, "http://my/rest/api");

Client client = new Client(Protocol.HTTP);

Response response = client.handle(request);

//get response representation and process


回答3:

If you only wish to invoke a REST service and get the response you can try out REST Assured:

// Make a GET request to "/lotto"
String json = get("/lotto").asString()
// Parse the JSON response
List<String> winnderIds = with(json).get("lotto.winners.winnerId");

// Make a POST request to "/shopping"
String xml = post("/shopping").andReturn().body().asString()
// Parse the XML
Node category = with(xml).get("shopping.category[0]");


回答4:

I am using resteasy as the rest frameworks and it works just fine and easy (both to rewrite and to test, same as easymock). As a sample code:

@Path("/webservice")

public class Web
{

    @GET
    @Path("{web}")
    @ProduceMime("application/xml")
    public String test(@QueryParam("param") String param, @PathParam("web") String web) 
    {
    //  code here
    }
}
  • @Path is your "root path" of the class (your real "root" would be configured on components.xml)
  • @GET is from Rest
  • ProduceMime or ConsumeMime is the mime you should consume or produce
  • @QueryParam is the params of the url and @PathParam the parameters you should get

So this get will receive a call from /webservice/web?param=lalala and return a string in the application/xml format



标签: java rest