如何在Servlet中调用java Rest WebService [英] How to Call java Rest WebService inside a Servlet
问题描述
我有一个java Rest WebService URL http:// localhost:8080 / WebServiceEx / rest / hello / dgdg
I have a java Rest WebService URL http://localhost:8080/WebServiceEx/rest/hello/dgdg
当我执行URL时,WebService方法返回一个字符串
When i execute the URL ,the WebService Method Returns a String
我的要求是在Servlet中调用上面的WebService URL,可以帮助吗?
My Requirement is to call the above WebService URL inside a Servlet ,Could any one Help?
ServletCode:
ServletCode:
public Class StoreServlet extends HttpServlet{
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
//Invoke WebService and Get Response String Here
}
WebService代码:
WebService Code:
public class HelloWorldService {
@Context
private ServletContext context;
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
return Response.status(200).entity(msg).build();
}
}
推荐答案
看看Apache CXF JAX-RS客户端:
Take a look at Apache CXF JAX-RS client:
http://cxf.apache.org/docs/jax-rs-client-api.html
例如
BookStore store = JAXRSClientFactory.create("http://bookstore.com", BookStore.class);
// (1) remote GET call to http://bookstore.com/bookstore
Books books = store.getAllBooks();
// (2) no remote call
BookResource subresource = store.getBookSubresource(1);
// {3} remote GET call to http://bookstore.com/bookstore/1
Book b = subresource.getBook();
或者,如果你使用JAX-RS 2.0,它有客户端API
Or, if you use JAX-RS 2.0, it has a client API
例如
Client client = ClientFactory.newClient();
String bal = client.target("http://.../atm/balance")
.queryParam("card", "111122223333")
.queryParam("pin", "9876")
.request("text/plain").get(String.class);
或者你可以使用普通的Java以核心的方式做到这一点: http://www.mkyong.com/webservices/jax- rs / restfull-java-client-with-java-net-url /
Or you can do it the "core" way using just plain Java: http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
这篇关于如何在Servlet中调用java Rest WebService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!