Jersey HTTP响应200 OK但返回内容错误(检查请求)

时间:2015-07-29 19:12:24

标签: java rest http jersey httprequest

这是the one I asked yesterday的后续问题。

登录页面应该在输入正确的用户名和密码后重定向到主页面,服务器端返回一个空字符串("")。如果其中任何一个不正确,服务器端代码将返回"Username or Password are incorrect"

页面功能运行良好但是当我使用正确的用户名和密码对我的客户端代码进行测试时,它返回"Username or Password are incorrect",响应返回200OK。

以下是我的客户端代码:

public static void main(String[] args){
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client.target("http://localhost:8080
                               /qa-automation-console").path("authenticate");
    Form form = new Form();
    form.param("username", "username");
    form.param("password", "password");
    Response response = target.request().post(Entity.form(form));
     //The response was 200OK.
    System.out.println(response.readEntity(String.class));
}

而不是其他问题,包括HTML和Web.xml依赖,现在我怀疑我写的客户端代码不正确,并且在发送请求时它不包含正确的用户名和密码。服务器端代码如下:

@POST
@Produces("text/plain")
@Path("authenticate")
public String authenticate(@Context HttpServletRequest req, @QueryParam("username") 
                      String username, @QueryParam("password") String password) 
                                                          throws Exception {
    Environments environments = new DefaultConfigurationBuilder().build();
    final ALMProfile profile = new ALMProfile();
    profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
    profile.setUsername(username);
    if ( !Strings.isNullOrEmpty(password) ) {
        String encryptedPassword = EncryptionUtils.encrypt(password);
        profile.setPassword(encryptedPassword);
    }
    try (ALMConnection connection = new ALMConnection(profile);) {
        if (connection.getOtaConnector().connected()) {
            req.getSession(true).setAttribute("username", username);
            req.getSession(true).setAttribute("password", profile.getPassword());
            return "";
        }
    } catch (Exception e) {
        e.printStackTrace();
        return "Username or Password are incorrect";
    }
    return "Username or Password are incorrect";
}

有人可以指出客户端代码是否提交了正确的请求吗?

1 个答案:

答案 0 :(得分:0)

首先,您可能需要检查堆栈跟踪中的内容。以及usernamepassword的值。我怀疑它们是空的。

其次,我认为问题来自@QueryParam注释。

您必须使用@FormParam

当您的网址包含params时,您使用QueryParam:

  

www.test.com/test?username=test

当您在表单中发送数据时,必须使用注释@FormParam

相关问题