如何在Servlet中调用JavaRESTWebService

如何在Servlet中调用JavaRESTWebService,java,web-services,rest,jakarta-ee,servlets,Java,Web Services,Rest,Jakarta Ee,Servlets,我有一个java Rest Web服务URLhttp://localhost:8080/WebServiceEx/rest/hello/dgdg 当我执行URL时,WebService方法返回一个字符串 我的要求是在Servlet中调用上述WebService URL,有人能帮忙吗 服务代码: public Class StoreServlet extends HttpServlet{ protected void doPost(HttpServletRequest req, HttpServ

我有一个java Rest Web服务URL
http://localhost:8080/WebServiceEx/rest/hello/dgdg

当我执行URL时,WebService方法返回一个字符串

我的要求是在Servlet中调用上述WebService URL,有人能帮忙吗

服务代码:

public Class StoreServlet extends HttpServlet{
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {

//Invoke WebService and Get Response String Here


} 
Web服务代码:

public class HelloWorldService {
    @Context 
    private ServletContext context;

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

                    return Response.status(200).entity(msg).build();    

                }
    }

一种可能是使用jaxws生成webservice客户机(为此,请在internet上查找教程)。因此,您可以在servlet中使用一些Java类

看看Apache CXF JAX-RS客户端:

e、 g

或者,如果您使用JAX-RS2.0,它有一个

e、 g

或者,您也可以使用纯Java以“核心”方式完成:

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();
Client client = ClientFactory.newClient();

String bal = client.target("http://.../atm/balance")
                   .queryParam("card", "111122223333")
                   .queryParam("pin", "9876")
                   .request("text/plain").get(String.class);