gpt4 book ai didi

java - 从另一个项目调用 REST Web 服务

转载 作者:行者123 更新时间:2023-11-30 03:09:36 25 4
gpt4 key购买 nike

我在一个名为 pmtv2 的项目中实现了一个 Rest Webservice java 类,我想从另一个名为 sigac 的项目中的另一个类中调用它,如图所示。 enter image description here

这里是pmtv2包中包含的WService类

package cat.diba.jee.pmtv2.ws.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cat.diba.jee.pmtv2.ws.rest.manager.RealitzarExpedientManager;
import cat.diba.jee.pmtv2.ws.rest.manager.RealitzarTramitManager;
import cat.diba.jee.pmtv2.ws.rest.message.RestMessage;
import cat.diba.jee.pmtv2.ws.rest.object.RespostaExpedient;
import cat.diba.jee.pmtv2.ws.rest.object.RespostaRealitzarTramit;
import cat.diba.jee.pmtv2.ws.rest.utils.TokenUtils;

/**
* The Class PmtRestWsService.
*/
@Path("/tramitacio")
public class PmtRestWsService {

/**
* The Constant CLASS_ID.
*/
private static final String CLASS_ID = PmtRestWsService.class.getName();

/**
* Log de la classe.
*/
private static final Log LOG = LogFactory.getLog(CLASS_ID);

/**
* The Constant PARAM_SESSION.
*/
private static final String PARAM_SESSION = "session";

/**
* The Constant PARAM_TOKEN.
*/
private static final String PARAM_TOKEN = "token";

/**
* The Constant PARAM_USERNAME.
*/
private static final String PARAM_USERNAME = "username";

/**
* The Constant PARAM_TRAMITS.
*/
private static final String PARAM_TRAMITS = "tramits";

/**
* The constants PARAM_EXPEDIENTS
*/
private static final String PARAM_EXPEDIENTS = "expedients";

/**
* Realitzar tramit.
*
* @param params the params
* @return the pmt expedient
*/
@POST
@Path("/realitzarTramit")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/json,application/xml")
public RespostaRealitzarTramit realitzarTramit(String params) {
LOG.debug("Parametres = " + params);

RealitzarTramitManager manager = new RealitzarTramitManager();
RespostaRealitzarTramit resposta = new RespostaRealitzarTramit();

JSONObject jsonObject = new JSONObject(params);
try {
if (validarParametresEntrada(jsonObject)) {
String session = jsonObject.getString(PARAM_SESSION);
String token = jsonObject.getString(PARAM_TOKEN);
if (TokenUtils.validarToken(session, token)) {
resposta = manager.realitzarTramits(jsonObject, jsonObject.getString(PARAM_USERNAME));
} else {
//Token no validat
resposta.setTramitOK(false);
resposta.setError(RestMessage.ERROR_TOKEN_INVALID.getMessage());
}
} else {
//Paràmetres invàlids
resposta.setTramitOK(false);
resposta.setError(RestMessage.ERROR_REALITZAR_TRAMIT_PARAMETRES_ENTRADA.getMessage());
}
} catch (Exception e) {
// Errors als paràmetres d'entrada
LOG.error("ERROR : " + e.getMessage() + " - ORIGEN : " + e.getStackTrace()[0]);
resposta.setTramitOK(false);
resposta.setError(RestMessage.ERROR_REALITZAR_TRAMIT_NO_CONTROLAT.getMessage());
return resposta;
}
return resposta;
}

/**
* Realitzar tramit.
*
* @param params the params
* @return the pmt expedient
*/
@POST
@Path("/expedient")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/json,application/xml")
public RespostaExpedient realitzarExpedient(String params) {
LOG.debug("Parametres = " + params);

RealitzarExpedientManager manager = new RealitzarExpedientManager();
RespostaExpedient resposta = new RespostaExpedient();

JSONObject jsonObject = new JSONObject(params);
try {
if (validarParametresEntradaExpedient(jsonObject)) {
String session = jsonObject.getString(PARAM_SESSION);
String token = jsonObject.getString(PARAM_TOKEN);
if (TokenUtils.validarToken(session, token)) {
resposta = manager.realitzarExpedients(jsonObject, jsonObject.getString(PARAM_USERNAME));
} else {
//Token no validat
resposta.setExpedientOK(false);
resposta.setCodiError(901);
resposta.setError(RestMessage.ERROR_TOKEN_INVALID.getMessage());
}
} else {
//Paràmetres invàlids
resposta.setExpedientOK(false);
resposta.setCodiError(902);
resposta.setError(RestMessage.ERROR_REALITZAR_EXPEDIENT_PARAMETRES_ENTRADA.getMessage());
}
} catch (Exception e) {
// Errors als paràmetres d'entrada
LOG.error("ERROR : " + e.getMessage() + " - ORIGEN : " + e.getStackTrace()[0]);
resposta.setExpedientOK(false);
resposta.setCodiError(998);
resposta.setError(RestMessage.ERROR_REALITZAR_EXPEDIENT_NO_CONTROLAT.getMessage());
return resposta;
}
return resposta;
}

/**
* validacio de entrada de expedients
*
* @param jsonObject
* @return
*/
private boolean validarParametresEntradaExpedient(JSONObject jsonObject) {
LOG.debug("validarPeticio(jsonObject) - Inici");

boolean result = true;
try {
jsonObject.getJSONArray(PARAM_EXPEDIENTS);
jsonObject.getString(PARAM_USERNAME);
jsonObject.getString(PARAM_SESSION);
jsonObject.getString(PARAM_TOKEN);
} catch (Exception e) {
result = false;
}

LOG.debug("validarParametresEntrada(jsonObject) - Fi");
return result;
}

/**
* Validar parametres entrada.
*
* @param jsonObject the json object
* @return true, if successful
*/
private boolean validarParametresEntrada(final JSONObject jsonObject) {
LOG.debug("validarPeticio(jsonObject) - Inici");

boolean result = true;
try {
jsonObject.getJSONArray(PARAM_TRAMITS);
jsonObject.getString(PARAM_USERNAME);
jsonObject.getString(PARAM_SESSION);
jsonObject.getString(PARAM_TOKEN);
} catch (Exception e) {
result = false;
}

LOG.debug("validarParametresEntrada(jsonObject) - Fi");
return result;
}
}

有什么办法可以做到吗?

最佳答案

JAX-RS 客户端 API

您可以尝试JAX-RS Client API ,它提供了用于访问任何 REST 资源的高级 API。客户端 API 在 javax.ws.rs.client 中定义包。

要使用客户端 API 访问 REST 资源,您需要执行以下步骤:

  1. 获取 javax.ws.rs.client.Client 的实例接口(interface)。
  2. 配置Client具有目标的实例。
  3. 根据目标创建请求。
  4. 调用请求。

示例

尝试以下操作来访问您的网络服务(只需根据您的需要更改 URI 路径):

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080")
.path("pmtv2")
.path("api")
.path("tramitacio")
.path("realitzarTramit");

RespostaExpedient response = target.request(MediaType.APPLICATION_JSON)
.post(Entity.json(data)), RespostaExpedient.class);

更多信息

您将需要 JAX-RS Client API 的实现,如JerseyRESTEasy .

关于java - 从另一个项目调用 REST Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com