使用Java 8 / Eclipse / JAX RS,使用简单的命令行客户端尝试触发托管在apache上的web服务时遇到问题。在我在apache中托管的Web服务中,我有一个类,它代表管理用户User Services的服务。它包含一个简单的方法,用于在发送昵称和邮件时返回User POJO类实例。传递方言的数据是JSON(我的老板爱上了JSON)。
@Path("/UserService")
public class UserService
{
@SuppressWarnings("finally")
@Path("/login")
@POST
@Produces(MediaType.APPLICATION_JSON)
public User login(@FormParam("nickname") String nickname, @FormParam("mail") String mail)
{
User loggedUser = null;
try
{
loggedUser = _resource.login(nickname, mail);
}
catch (RequesterException e)
{
_logger.write(e);
}
catch (ConnectionException e)
{
_logger.write(e);
}
finally
{
return loggedUser;
}
}
}
在命令行客户端的一个非常简单的类中,我尝试推送有效的昵称和邮件以获取现有用户。我的URI很好,我的web服务在apache tomcat中正确启动。
public class JSONTest
{
private static final String REST_URI = "http://localhost:8080/WebServices/Texte/UserService";
public User getloggedUser(String nickname, String mail)
{
Client client = ClientBuilder.newClient();
Form form = new Form().param("nickname", nickname).param("mail", mail);
return client
.target(REST_URI)
.path( "/login" )
.request(MediaType.APPLICATION_JSON)
.post( Entity.form(form), Player.class);
}
}
在作为入口点的主要方法中,我启动了期待现有用户的测试。
public static void main(String[] args)
{
JSONTest j = new JSONTest();
User maxUser = j.getloggedUser("Max", "max@max.com");
}
但是我得到了一个很大的例外:我不明白因为我将表单发布到web服务期望带有表单参数的帖子。我当然在某个地方犯了一个错误,但在搜索和尝试了很多事情后,我被困住了。
Exception in thread "main" javax.ws.rs.NotAllowedException: HTTP 405 Method Not Allowed at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1011)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)