好吧,我创建了一个 RESTFUL Web 服务,其中包含一个基于帐户 ID 返回银行交易列表的 get 方法。 当我测试时,该服务运行良好。
然后,我在托管Bean中创建了一个Restful客户端。 当我调用我的 Web 服务时会弹出此异常。
我已经尝试了所有客户端列表(没有参数(的此方法,并且它有效,但是当我向我的事务方法添加参数时,无法从我的请求中提取 ID。
这是我的网络服务
@Stateless
@Path("/transactions")
public class TransactionWebService {
@EJB
private GestionAccountsLocal local1;
@EJB
private GestionTransactionsLocal local2;
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public List<Transaction> getTransactionById(@PathParam(value="id") Integer id)
{
Account account=local1.findAccountByid(id);
return local2.showTransactionByAccount(account);
}
}
这是我的安息客户
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.json.JsonException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import org.json.JSONArray;
@ManagedBean
@SessionScoped
public class WebServiceTransaction {
private WebTarget webtarget;
private Client client;
private entities.Account account;
private Transaction transaction;
private List<Transaction> transactions;
private static final String base_url= "http://localhost:8383/ebankingWEB/rest/transactions" ;
public WebTarget getWebtarget() {
return webtarget;
}
public void setWebtarget(WebTarget webtarget) {
this.webtarget = webtarget;
}
public Transaction getTransaction() {
return transaction;
}
public void setTransaction(Transaction transaction) {
this.transaction = transaction;
}
public static String getBaseUrl() {
return base_url;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public WebServiceTransaction() {
client= ClientBuilder.newClient();
webtarget=client.target(base_url).path("id");
}
public List<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}
public List<Transaction> convertToList (String jsonListString) throws JsonException{
try{
JSONArray jsonList = new JSONArray(jsonListString);
transactions = new ArrayList<Transaction>();
for (int i=0 ; i < jsonList.length();i++){
Transaction transaction = new Transaction(jsonList.get(i).toString());
transactions.add(transaction);
}
return transactions;
}
catch (Exception e) {
throw new JsonException(e.getMessage());
}
}
public List<Transaction> getTransactionsByAccount (Integer id){
try{
WebTarget ressource = webtarget;
ressource.path(MessageFormat.format("{0}" , new Object[]{id}));
String jsonListString = ressource.request(MediaType.APPLICATION_JSON).get(String.class);
return convertToList(jsonListString);
}
catch (Exception e){
String message = "non existant transactions ";
FacesContext.getCurrentInstance()
.addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
message, null));
e.printStackTrace();
return new ArrayList<Transaction>();
}
}
public entities.Account getAccount() {
return account;
}
public void setAccount(entities.Account account) {
this.account = account;
}
}
这是我的日志
15:51:05,177 WARN [org.jboss.resteasy.core.ExceptionHandler] (default task-26) failed to execute: javax.ws.rs.NotFoundException: Unable to extract parameter from http request: javax.ws.rs.PathParam("id") value is 'id' for public java.util.List webServices.TransactionWebService.getTransactionById(java.lang.Integer)
at org.jboss.resteasy.core.PathParamInjector$1.throwProcessingException(PathParamInjector.java:54) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.StringParameterInjector.extractValue(StringParameterInjector.java:336) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.PathParamInjector.inject(PathParamInjector.java:131) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:89) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:112) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.8.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.8.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_75]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_75]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_75]
Caused by: java.lang.NumberFormatException: For input string: "id"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) [rt.jar:1.7.0_75]
at java.lang.Integer.parseInt(Integer.java:492) [rt.jar:1.7.0_75]
at java.lang.Integer.<init>(Integer.java:677) [rt.jar:1.7.0_75]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_75]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) [rt.jar:1.7.0_75]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_75]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) [rt.jar:1.7.0_75]
at org.jboss.resteasy.core.StringParameterInjector.extractValue(StringParameterInjector.java:319) [resteasy-jaxrs-3.0.8.Final.jar:]
... 38 more
15:51:05,191 ERROR [stderr] (default task-25) javax.ws.rs.NotFoundException: HTTP 404 Not Found
15:51:05,191 ERROR [stderr] (default task-25) at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:181)
15:51:05,191 ERROR [stderr] (default task-25) at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:154)
15:51:05,191 ERROR [stderr] (default task-25) at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:444)
15:51:05,192 ERROR [stderr] (default task-25) at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:165)
15:51:05,192 ERROR [stderr] (default task-25) at tn.esprit.webServiceData.WebServiceTransaction.getTransactionsByAccount(WebServiceTransaction.java:89)
15:51:05,192 ERROR [stderr] (default task-25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15:51:05,192 ERROR [stderr] (default task-25) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
15:51:05,192 ERROR [stderr] (default task-25) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
15:51:05,192 ERROR [stderr] (default task-25) at java.lang.reflect.Method.invoke(Method.java:606)
15:51:05,193 ERROR [stderr] (default task-25) at javax.el.ELUtil.invokeMethod(ELUtil.java:308)
15:51:05,193 ERROR [stderr] (default task-25) at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
15:51:05,193 ERROR [stderr] (default task-25) at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
15:51:05,193 ERROR [stderr] (default task-25) at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
15:51:05,193 ERROR [stderr] (default task-25) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
15:51:05,193 ERROR [stderr] (default task-25) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
15:51:05,194 ERROR [stderr] (default task-25) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
15:51:05,194 ERROR [stderr] (default task-25) at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
15:51:05,194 ERROR [stderr] (default task-25) at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
15:51:05,194 ERROR [stderr] (default task-25) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
15:51:05,194 ERROR [stderr] (default task-25) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
15:51:05,194 ERROR [stderr] (default task-25) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
15:51:05,194 ERROR [stderr] (default task-25) at javax.faces.component.UICommand.broadcast(UICommand.java:315)
15:51:05,195 ERROR [stderr] (default task-25) at javax.faces.component.UIData.broadcast(UIData.java:1108)
15:51:05,195 ERROR [stderr] (default task-25) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
15:51:05,195 ERROR [stderr] (default task-25) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
15:51:05,195 ERROR [stderr] (default task-25) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
15:51:05,195 ERROR [stderr] (default task-25) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
15:51:05,195 ERROR [stderr] (default task-25) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
15:51:05,196 ERROR [stderr] (default task-25) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
15:51:05,196 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
15:51:05,196 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
15:51:05,196 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
15:51:05,196 ERROR [stderr] (default task-25) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
15:51:05,196 ERROR [stderr] (default task-25) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
15:51:05,197 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
15:51:05,197 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
15:51:05,197 ERROR [stderr] (default task-25) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
15:51:05,197 ERROR [stderr] (default task-25) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
15:51:05,197 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
15:51:05,197 ERROR [stderr] (default task-25) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
15:51:05,198 ERROR [stderr] (default task-25) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
15:51:05,198 ERROR [stderr] (default task-25) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
15:51:05,198 ERROR [stderr] (default task-25) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
15:51:05,198 ERROR [stderr] (default task-25) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
15:51:05,198 ERROR [stderr] (default task-25) at java.lang.Thread.run(Thread.java:745)
附注:我认为这一行是问题所在,因为它获取了基本 URL,但 ID 未正确提取webtarget=client.target(base_url).path("id");
如果您需要其他任何东西,请告诉我。
您使用
@Path("/{id}")
所以你不应该在构造函数的webtaregt中提到路径片段"/id":
webtarget=client.target(base_url).path("id"); // wrong
webtarget=client.target(base_url); // OK
例如:考虑到交易 id=1 存在,请检查您的网络浏览器:
../transactions/id/1 //wrong
../transactions/1 // OK
我更正了我的 REST 客户端,它就像一个魅力。
webtarget=client.target(base_url);
和
WebTarget ressource = webtarget;
ressource = ressource.path(MessageFormat.format("{0}" , new Object[]{id}));
在此之前,我没有影响对参数的重新资源。