[英]How to Call java Rest WebService inside a Servlet
我有一个java Rest WebService URL http://localhost:8080/WebServiceEx/rest/hello/dgdg
当我执行URL时,WebService方法返回一个String
我的要求是在Servlet中调用上面的WebService URL,可以任何一个帮助吗?
ServletCode:
public Class StoreServlet extends HttpServlet{
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
//Invoke WebService and Get Response String Here
}
WebService代码:
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客户端:
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
例如
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/
一种可能性是使用jaxws生成web服务客户端(为此目的 - 在互联网上查找教程)。 因此,您可以获得一些可以在servlet中使用的Java类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.