如何摆脱响应中的json属性“variant”,“annotations”

时间:2015-08-06 10:01:52

标签: annotations jersey dropwizard variant

我已经开始使用dropwizard开发REST服务器。问题作为长资源方法返回一个EntityType(比如注册),输出是预期的,但我决定使用下面的代码发送自定义状态代码 Response.status(Response.Status.PRECONDITION_FAILED) .entity(Entity.json(new enrolment, AdapterResponseStatus.FAILURE))) .build();

一切都很好但输出现在包含一些额外的额外属性,如下所示。

{ "entity": { "id": 1267, "courseId": "5798890", "userName": "user@abc.com", "tenantId": "tenant1", "status": "approved", "link": "/enrollments/null" }, "variant": { "language": null, "mediaType": { "type": "application", "subtype": "json", "parameters": { }, "wildcardType": false, "wildcardSubtype": false }, "encoding": null, "languageString": null }, "annotations": [ ], "mediaType": { "type": "application", "subtype": "json", "parameters": { }, "wildcardType": false, "wildcardSubtype": false }, "language": null, "encoding": null }

我一直在期待“实体”属性,但却获得了其他属性。有没有人摆脱它们,因为没有人会消耗它们?

即使我用空字符串替换我的实体对象(注册),也会出现这些标记。

1 个答案:

答案 0 :(得分:1)

如果您查看ResponseBuilder entity方法的签名,则直接获取该对象;与Jersey客户端不同,后者需要一个特殊的Entity对象,其中包含annotationsvariants个字段。

将您的代码更改为:

Response.status(Response.Status.PRECONDITION_FAILED)
                .entity(new Enrolment())
                .build();