【问题标题】:How to Call java Rest WebService inside a Servlet如何在 Servlet 中调用 java Rest WebService
【发布时间】:2013-06-22 05:16:20
【问题描述】:

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

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

我的要求是在 Servlet 中调用上面的 WebService URL,有谁能帮忙吗?

Servlet代码:

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

//Invoke WebService and Get Response String Here


} 

网络服务代码:

public class HelloWorldService {
    @Context 
    private ServletContext context;

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

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

                }
    }

【问题讨论】:

    标签: java web-services rest jakarta-ee servlets


    【解决方案1】:

    看看 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,它有一个 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/

    【讨论】:

      【解决方案2】:

      一种可能性是使用 jaxws 生成 web 服务客户端(为此目的 - 在 Internet 上查找教程)。因此,您可以获得一些可以在 servlet 中使用的 Java 类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-04
        • 2012-10-16
        • 2017-06-08
        • 1970-01-01
        • 2012-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多