Java 网络 Restful 客户端不工作

Java web Restful client don't work

网络客户端出现错误java,但我不明白出现这些错误的原因。

服务器项目上的剩余资源

package model.rest;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@Stateless
@Path("/factorial")

public class FactorialResource {
@GET    
public String factorial(@QueryParam("base") long base)    {
return Long.toString($factorial(base));
}
long $factorial(long base){
if(base>=1){
return $factorial(base-1)*base;
}
return 1;
}
}

在导入 Restful java 客户端时,到项目 java ee 客户端。 第 49 行 return resource.get (String.class) 出现错误; 找不到符号符号:方法 get(Class) location:WebTarget

类型的变量资源
package Webservicce;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;

/**
* Jersey REST client generated for REST resource:FactorialResource
* [/factorial]<br>
* USAGE:
* <pre>
*        ClientRest client = new ClientRest();
*        Object response = client.XXX(...);
*        // do whatever with response
*        client.close();
* </pre>
*
* @author x2010s
*/
public class ClientRest {

private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/SimpleRESTweb/webresources";

public ClientRest() {
    client = javax.ws.rs.client.ClientBuilder.newClient();
    webTarget = client.target(BASE_URI).path("factorial");
}

public String factorial(String base) throws ClientErrorException {
    WebTarget resource = webTarget;
    if (base != null) {
        resource = resource.queryParam("base", base);
    }
    return resource.get(String.class);
}

public void close() {
    client.close();
}

}

servlet 原因:实际和形式参数列表的长度不同 processRequest (request, response) 行中的 doGet 和 doPost 均出现错误; 原因:当前和正式参数列表的长度不同

package controlador;

import Webservicce.ClientRest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author x2010s
*/
@WebServlet(name = "Calcular", urlPatterns = {"/calcular.do"})
public class Calcular extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and     <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
 protected void processRequest(HttpServletRequest request, HttpServletResponse response, ClientRest factorial, String base)
        throws ServletException, IOException {
    ClientRest rest = new ClientRest();

    request.setAttribute("factorial",factorial);
    request.getRequestDispatcher("index.jsp").forward(request, response);

  }

  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
 /**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
 @Override
 public String getServletInfo() {
    return "Short description";
 }// </editor-fold>

 }

我正在查看您代码中的以下代码片段,根据 this class WebTarget.java 中没有 get 方法,我不知道为什么使用它。

WebTarget resource = webTarget;
    if (base != null) {
        resource = resource.queryParam("base", base);
    }
    return resource.get(String.class);

看起来您正在为某些 Jersey 版本使用生成器,这与您的依赖项不同。

如果您使用的是 Jersey 2.x 那么我想这会起作用:

public String factorial(String base) throws ClientErrorException {
    WebTarget resource = webTarget;
    if (base != null) {
        resource = resource.queryParam("base", base);
    }
    return resource.request().get(String.class);  // <-- changed
}

关于您的 servlet,您应该检查您的方法签名:processRequest(request, response); 未在任何地方声明。