Java web Restful client don't work

时间:2018-04-20 00:56:49

标签: java web-services jsp restful-architecture

errors appear in the java web client, but I do not understand the reason why those errors appear.

the the rest resource on the server project

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;
}
}

when importing the Restful java client, to the project java ee client. an error appears on the line 49 return resource.get (String.class); cannot find symbolsymbol: metho get(Class) location: variable resource of type 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();
}

}

the servlet reason: actual and formal argument lists differ in length an error appears in doGet and doPost in both in the line processRequest (request, response); reason: current and formal argument lists differ in length

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>

 }

2 个答案:

答案 0 :(得分:0)

I was looking into the below snippet in your code, and according to this there is no get method in class WebTarget.java and I don't know why you use it.

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

答案 1 :(得分:0)

看起来你正在为某些泽西岛版本使用生成器,这与你的依赖版本不同。

如果您正在使用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);未在任何地方声明。