Java API for invoking REST services [closed]

2019-05-12 04:12发布

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

标签: java rest
4条回答
小情绪 Triste *
2楼-- · 2019-05-12 04:32

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

查看更多
Ridiculous、
3楼-- · 2019-05-12 04:41

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
查看更多
混吃等死
4楼-- · 2019-05-12 04:41

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]");
查看更多
仙女界的扛把子
5楼-- · 2019-05-12 04:54

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.

查看更多
登录 后发表回答