测试POST防止status=405的方法有哪些?
What are the methods testing POST to prevent status=405?
我正在尝试使用 Jersey 构建 RESTful 网络服务。
在我的服务器端代码中,有一个名称为 "domain" 的路径,我用它来显示内容。 "domain" 所指的页面内容只有输入正确的用户名和密码才能访问。
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("domain")
public ArrayList<String> domainList(@Context HttpServletRequest req) throws Exception{
Environments environments = new DefaultConfigurationBuilder().build();
final ALMProfile profile = new ALMProfile();
profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
profile.setUsername((String) req.getSession().getAttribute("username"));
//Set username from input, HTML form
profile.setPassword((String) req.getSession().getAttribute("password"));
//Set password from input, HTML form
try (ALMConnection connection = new ALMConnection(profile);) {
if (connection.getOtaConnector().connected()) {
Multimap<String, String> domain = connection.getDomains();
ArrayList<String> domain_names = new ArrayList<String>();
for(String key : domain.keys()){
if(domain_names.contains(key)) domain_names.add(key);
}
return domain_names; //return the content
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
当我试图测试是否返回了正确的内容时,我得到了一个错误(status=405,原因=Method Not Allowed)。下面是我的客户端测试。
public static void main(String[] args){
Environments environments = new DefaultConfigurationBuilder().build();
final ALMProfile profile = new ALMProfile();
profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
profile.setUsername("username"); //Creating a profile with username and password
profile.setPassword("password");
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
String response = target.path("domain").request().accept
(MediaType.APPLICATION_JSON).get(Response.class).toString();
//Above is the GET method I see from an example,
//probably is the reason why 405 error comes from.
System.out.println(response);
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/qa-automation-console").build();
}
servlet 配置良好。我们有其他路径成功 运行。
我怀疑原因可能来自我使用 GET 方法来完成本应 POST 的工作。
但是我不熟悉我可以使用的 Jersey 方法。
有人知道我可以用来测试功能的方法吗?
405 Method Not Allowed
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
您的端点用于 @POST
请求。在您的客户端中,您正在尝试 get()
.
参见Client API documentation for information on how to make a POST request。如果 是 应该是一个 GET 请求,那么只需将方法注释更改为 @GET
.
另请注意,对于您的 @POST
资源方法,您应该始终在该方法支持的媒体类型上放置一个 @Consumes
注释。如果客户端发送不支持的媒体类型,那么他们将按预期收到 415 not supported。我会 posted 一个客户端的例子 post,但我不知道你想要什么类型,因为缺少注释,而且你甚至没有 post对象作为方法参数,所以我什至不确定你的方法是否真的应该用于 POST.
另请参阅:
- How to send json object from REST client using javax.ws.rs.client.WebTarget
我正在尝试使用 Jersey 构建 RESTful 网络服务。
在我的服务器端代码中,有一个名称为 "domain" 的路径,我用它来显示内容。 "domain" 所指的页面内容只有输入正确的用户名和密码才能访问。
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("domain")
public ArrayList<String> domainList(@Context HttpServletRequest req) throws Exception{
Environments environments = new DefaultConfigurationBuilder().build();
final ALMProfile profile = new ALMProfile();
profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
profile.setUsername((String) req.getSession().getAttribute("username"));
//Set username from input, HTML form
profile.setPassword((String) req.getSession().getAttribute("password"));
//Set password from input, HTML form
try (ALMConnection connection = new ALMConnection(profile);) {
if (connection.getOtaConnector().connected()) {
Multimap<String, String> domain = connection.getDomains();
ArrayList<String> domain_names = new ArrayList<String>();
for(String key : domain.keys()){
if(domain_names.contains(key)) domain_names.add(key);
}
return domain_names; //return the content
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
当我试图测试是否返回了正确的内容时,我得到了一个错误(status=405,原因=Method Not Allowed)。下面是我的客户端测试。
public static void main(String[] args){
Environments environments = new DefaultConfigurationBuilder().build();
final ALMProfile profile = new ALMProfile();
profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
profile.setUsername("username"); //Creating a profile with username and password
profile.setPassword("password");
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
String response = target.path("domain").request().accept
(MediaType.APPLICATION_JSON).get(Response.class).toString();
//Above is the GET method I see from an example,
//probably is the reason why 405 error comes from.
System.out.println(response);
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/qa-automation-console").build();
}
servlet 配置良好。我们有其他路径成功 运行。 我怀疑原因可能来自我使用 GET 方法来完成本应 POST 的工作。 但是我不熟悉我可以使用的 Jersey 方法。
有人知道我可以用来测试功能的方法吗?
405 Method Not Allowed
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
您的端点用于 @POST
请求。在您的客户端中,您正在尝试 get()
.
参见Client API documentation for information on how to make a POST request。如果 是 应该是一个 GET 请求,那么只需将方法注释更改为 @GET
.
另请注意,对于您的 @POST
资源方法,您应该始终在该方法支持的媒体类型上放置一个 @Consumes
注释。如果客户端发送不支持的媒体类型,那么他们将按预期收到 415 not supported。我会 posted 一个客户端的例子 post,但我不知道你想要什么类型,因为缺少注释,而且你甚至没有 post对象作为方法参数,所以我什至不确定你的方法是否真的应该用于 POST.
另请参阅:
- How to send json object from REST client using javax.ws.rs.client.WebTarget