当前位置: 动力学知识库 > 问答 > 编程问答 >

java - best practice invoking webservices

问题描述:

I'm doing a school project where we are supposed to tap into an existing database using web services. I'm trying to figure out what best practice is when calling a web service from back end. I've been working with Servlets before and I know them pretty well.

This is my web service that front end uses and I need to know what the best practice is when calling an external web service within my application:

@GET

@Path("requestAccess")

public void getAccessToken(@Context HttpServletRequest request, @Context HttpServletResponse response)

{

String code = request.getParameter("code");

String clientId = "4_4inr3rkl04ys44kowwgoo0w4g8gow0s84cw0o0cg48kogsosc0";

String clientSecret = "4cxgpduio8isgcc8oggsw8wkcokcksk8wc8cc088w4w40owg4s";

String redirectUrl = "http://localhost:8080/salesgoals/rest/oauth/requestAccess";

try {

URL url = new URL("https://.../oauth/v2/token?client_id="+clientId+"&client_secret="+clientSecret+"&grant_type=authorization_code&redirect_uri="+redirectUrl+"&code="+code);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));

String strTemp = "";

while(null != (strTemp = bufferedReader.readLine())){

System.out.println(strTemp);

}

} catch(Exception e){e.printStackTrace();}

}

This returns me an access token and the results from the external web service are JSON objects and the url object with inputstreamReader is something temp. I used to see if I was successful receiving access token.

Now I need to use the access token to invoke external web services but I dont know what best practice is when it comes to calling it within the application. I'm used to HttpServletRequest/Response objects. As I said all the data is returned as json.

网友答案:

I doubt there is 100% best practice, but take a look at Jersey

It is both client and server RESTful web services implementation.

分享给朋友:
您可能感兴趣的文章:
随机阅读: