Java 服务无法识别重新发布的内容类型应用程序/json
我有一个服务,我可以使用以下jQuery代码访问它(来自GoogleChrome,使用--disable web security) 第一个调用设置cookie,这是第二个调用进行身份验证所必需的。这很好,两个请求都返回预期的结果 我正在尝试为该服务设置自动测试,并使用Restassed用JAVA编写Java 服务无法识别重新发布的内容类型应用程序/json,java,jquery,ajax,rest,rest-assured,Java,Jquery,Ajax,Rest,Rest Assured,我有一个服务,我可以使用以下jQuery代码访问它(来自GoogleChrome,使用--disable web security) 第一个调用设置cookie,这是第二个调用进行身份验证所必需的。这很好,两个请求都返回预期的结果 我正在尝试为该服务设置自动测试,并使用Restassed用JAVA编写 public class UserApplication { public static Map<String, String> authCookies = null;
public class UserApplication {
public static Map<String, String> authCookies = null;
public static String JSESSIONID = null;
public static void main(String[] args) {
Response resp = hello();
resp = apiUserApplication();
}
public static Response apiUserApplication() {
String userAppl = "http://10.30.1.2:9234/myapp/v6/user/appl/Firstname/Lastname/email@address.com/1998-01-01";
Response response = RestAssured.given()
.cookie("JSESSIONID", JSESSIONID).and()
.header("Accept", "application/json").and()
.header("Content-Type", "application/json").and()
.when().get(userAppl);
return response;
}
public static Response hello() {
String helloUrl = "http://10.30.1.2:9234/myapp/v6/hello";
Response response = RestAssured.given().cookies(authCookies)
.contentType("application/json").when().get(helloUrl);
return response;
}
}
看起来
JSESSIONID
和authCookies
被初始化为null
,并且在此代码中没有更改 我对重新启动没有经验
,但您的代码设置为首先调用hello
,然后调用apiUserApplication
。有些类级别的变量默认为null
,但从未显式地给它们赋值。特别是,在第二个调用中,apiUserApplication
,您将cookie的值设置为空值
我也不明白为什么要返回这个响应对象?如果您要检查它,确保响应中的数据符合您的预期,然后为第二个请求设置此会话ID,这将是有意义的。您是否尝试使用
.contentType
方法指定内容类型?@ilya是的,我尝试过,它的行为完全相同。您能检查一下其他apiUserApplication属性是否被转移了吗?是否有错误日志?在这段代码中,JSSessionID和AuthCookie似乎被初始化为null,并且没有更改。这可能是一个原因吗?
public class UserApplication {
public static Map<String, String> authCookies = null;
public static String JSESSIONID = null;
public static void main(String[] args) {
Response resp = hello();
resp = apiUserApplication();
}
public static Response apiUserApplication() {
String userAppl = "http://10.30.1.2:9234/myapp/v6/user/appl/Firstname/Lastname/email@address.com/1998-01-01";
Response response = RestAssured.given()
.cookie("JSESSIONID", JSESSIONID).and()
.header("Accept", "application/json").and()
.header("Content-Type", "application/json").and()
.when().get(userAppl);
return response;
}
public static Response hello() {
String helloUrl = "http://10.30.1.2:9234/myapp/v6/hello";
Response response = RestAssured.given().cookies(authCookies)
.contentType("application/json").when().get(helloUrl);
return response;
}
}
{"errors":["Content type 'null' not supported"]}