Jersey测试-具有JSON请求的Http Delete方法 [英] Jersey test - Http Delete method with JSON request

查看:243
本文介绍了Jersey测试-具有JSON请求的Http Delete方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey测试来测试Rest Service的DELETE方法:

I am using Jersey Test to test Rest service DELETE method:

@DELETE
@Path("/myPath")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MyResponse myMethod(MyRequest myRequest) {

我尝试了以下示例和其他方法:

I have tried the example below and other methods:

Entity<MyRequest> requestEntity = Entity.entity(new MyRequest(arg1, arg2), MediaType.APPLICATION_JSON);

target(MY_URI).request(MediaType.APPLICATION_JSON).method("DELETE", requestEntity)

target(MY_URI).request(MediaType.APPLICATION_JSON).build("DELETE", requestEntity).invoke();

但是它不起作用.

如何在Jersey测试中进行Http删除?

How to make Http Delete in Jersey test?

推荐答案

根据HTTP规范

如果DELETE请求中包含实体主体,则该主体将被忽略

If a DELETE request includes an entity body, the body is ignored

尽管许多服务器仍支持实体主体,但由于泽西岛的存在,我认为该主体违反了HTTP法规遵从性.泽西岛验证是否符合客户要求.要解决此验证问题,您可以设置客户端属性

Though a lot servers still support the entity body, I guess because of this Jersey considers the body as breaking HTTP Compliance. Jersey validates compliance with client requests. To get around this validation, you can set the client property

ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION a>

ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION

如果为true,将取消对HTTP规范合规性的严格验证.

If true, the strict validation of HTTP specification compliance will be suppressed.

默认情况下,Jersey客户端运行时会执行某些HTTP遵从性检查(例如哪些HTTP方法可以促进非空请求实体等),以便在用户尝试建立不符合HTTP的通信时快速失败并发生异常规格.需要重写这些合规性检查并避免由于某些原因而导致Jersey客户端运行时引发异常的用户,可以将此属性设置为true.结果,合规性问题将仅在日志中报告,并且不会引发任何异常.

By default, Jersey client runtime performs certain HTTP compliance checks (such as which HTTP methods can facilitate non-empty request entities etc.) in order to fail fast with an exception when user tries to establish a communication non-compliant with HTTP specification. Users who need to override these compliance checks and avoid the exceptions being thrown by Jersey client runtime for some reason, can set this property to true. As a result, the compliance issues will be merely reported in a log and no exceptions will be thrown.

请注意,该属性禁止显示Jersey层异常.不合规的行为很可能导致底层I/O连接器层中引发不同的异常集.

Note that the property suppresses the Jersey layer exceptions. Chances are that the non-compliant behavior will cause different set of exceptions being raised in the underlying I/O connector layer.

可以在客户端运行时配置中配置此属性,也可以直接在单个请求上配置此属性.如果发生冲突,特定于请求的属性值将优先于运行时配置中配置的值.

This property can be configured in a client runtime configuration or directly on an individual request. In case of conflict, request-specific property value takes precedence over value configured in the runtime configuration.

默认值为false.

The default value is false.

要在JerseyTest中进行配置,您可以

To configure it in JerseyTest, you can do

@Override
public void configureClient(ClientConfig config) {
   config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
}

假设您正在通过调用JerseyTesttarget(..)方法发出请求,则上述配置将适用于所有请求.如果只想删除某些请求的验证,也可以在WebTarget上设置该属性,而不进行上述配置.

Assuming you are making your requests by calling the target(..) method of the JerseyTest, the above configuration will be for all request. If you just want to remove the validation for certain requests, you can also set the property on the WebTarget and not do the above configuration.

target(...).property(...).request()...


编辑

我可能要提到的另一件事是,灰熊是不支持实体的服务器之一, 除非已配置.我不太确定如何在JerseyTest中进行配置.因此,如果您使用的是Grizzly测试提供程序,它甚至可能无法在服务器端使用.


EDIT

Another thing I might mention is that Grizzly is one of the servers that doesn't support the entity, unless configured. I'm not quite sure though how to configure that in JerseyTest. So if you are using the Grizzly test provider, it may not even work on the server side.

在这种情况下,您尝试使用内存中的测试提供程序,或使用码头的提供程序

If this is the case, you try to use the in-memory test provider, or use the jetty provider

这篇关于Jersey测试-具有JSON请求的Http Delete方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆