我试图创建一个客户端服务器 web api,其中客户端调用 web 服务,比如 web1,然后 web1 成为另一个 web 服务的客户端,比如 web2。web2 返回到 web1,因此客户端执行。我的目标是创建一个 Web 服务组合并将结果打印为两个 Web 服务的提交。
在这样做时我遇到了这个错误:
line 1:0 no viable alternative at character 'H'
line 1:1 no viable alternative at character 'e'
line 1:2 no viable alternative at character 'l'
line 1:3 no viable alternative at character 'l'
line 1:4 no viable alternative at character 'o'
line 1:5 no viable alternative at character 'p'
line 1:6 no viable alternative at character 'r'
line 1:7 no viable alternative at character 'i'
line 1:8 no viable alternative at character 'y'
line 1:9 no viable alternative at character 'a'
line 0:-1 no viable alternative at input '<EOF>'
Exception in thread "main" javax.ws.rs.WebApplicationException: HTTP 400 Bad
Request
这里 'hello' 是我调用getMessage()时应该运行的消息,而 'priya' 是在getName()上调用的名称
代码是:
//客户
public class MessageClient {
public static void main(String[] args) {
Client client= ClientBuilder.newClient();
WebTarget baseTarget =
client.target("http://localhost:8080/messenger/webapi");
WebTarget messageTarget = baseTarget.path("messages");
WebTarget singleMsgTarget = messageTarget.path("{messageId}");
Message message1= singleMsgTarget
.resolveTemplate("messageId", "1")
.request()
.get(Message.class);
System.out.println(message1.getMessage());
}
}
//web1(这是web2的客户端)
@Path("/messages")
public class MessageResources {
MessageService ms = new MessageService();
@GET
@Path("{messageId}")
@Produces(MediaType.APPLICATION_JSON)
public String getMessage(@PathParam("messageId") long id)
{
Client client= ClientBuilder.newClient();
WebTarget baseTarget=
client.target("http://localhost:8080/messenger/webapi");
WebTarget infoTarget= baseTarget.path("/info");
WebTarget singleInfoTarget= infoTarget.path("{infoId}");
InfoCreated info= singleInfoTarget
.resolveTemplate("infoId", "1")
.request()
.get(InfoCreated.class);
String name = info.getName();
Message message = ms.getMessage(id);
String msg=message.getMessage();
String result = msg.concat(name);
return result;
}
代码中没有错误,但是在运行时出现错误。