【问题标题】:why Resonse.ok().build() return the Response entity itself?为什么 Response.ok().build() 返回 Response 实体本身?
【发布时间】:2018-07-26 22:04:42
【问题描述】:

我有一个简单的 REST API,我只返回 Resonse.ok().build(),因为函数的主体是异步的

我期待一个带有 200 http 状态代码的空响应,但我得到了一个完整的描述,似乎是作为实体的响应调用。

我做错了什么?

这是我从 API 调用收到的 json 响应

{
    "context": {
        "headers": {},
        "entity": null,
        "entityType": null,
        "entityAnnotations": [],
        "entityStream": {
            "committed": false,
            "closed": false
        },
        "length": -1,
        "language": null,
        "location": null,
        "committed": false,
        "mediaType": null,
        "allowedMethods": [],
        "links": [],
        "entityTag": null,
        "stringHeaders": {},
        "lastModified": null,
        "date": null,
        "acceptableMediaTypes": [
            {
                "type": "*",
                "subtype": "*",
                "parameters": {},
                "quality": 1000,
                "wildcardType": true,
                "wildcardSubtype": true
            }
        ],
        "acceptableLanguages": [
            "*"
        ],
        "entityClass": null,
        "responseCookies": {},
        "requestCookies": {},
        "lengthLong": -1
    },
    "status": 200,
    "length": -1,
    "language": null,
    "location": null,
    "metadata": {},
    "cookies": {},
    "mediaType": null,
    "allowedMethods": [],
    "links": [],
    "statusInfo": "OK",
    "entityTag": null,
    "stringHeaders": {},
    "entity": null,
    "lastModified": null,
    "date": null,
    "headers": {}
}

REST api 看起来像这样

@RestController
@RequestMapping("/accountworkers")
@Api(value = "/updater")
public class AccountUpdater {

    private static Logger LOGGER = LoggerFactory.getLogger(AccountUpdater.class);

    @Autowired
    private AccountUpdaterController auController;

    @RequestMapping(value = "/updater", method = RequestMethod.GET)
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response runUpdater() {
        LOGGER.info("Running account updater");
        auController.doAccountUpdateAsync();
        return Response.ok().build();
    }
}

【问题讨论】:

    标签: rest spring-mvc jax-rs


    【解决方案1】:

    您正在尝试混合使用 JAX-RS 和 Spring MVC。这些不是一回事,也不兼容。通过返回 Spring MVC 无法识别的 Response,它像对任何其他实体一样对其进行序列化。如果您使用的是 Spring MVC,那么您希望使用 ResponseEntity

    return ResponseEntity.ok().build()
    

    如果您使用的是 Spring MVC,则应删除 Jersey/JAX-RS 的所有依赖项,这样您就不会对可以使用和不可以使用的内容感到困惑。

    除此之外,@Produces 也适用于 JAX-RS。对于 Spring MVC,您应该在 @RequestMapping 注释中添加产品。

    @RequestMapping(value="/", produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    

    【讨论】:

    • 感谢您的解释,那么在 JAX-RS 中执行此操作的正确方法是什么?我怎么知道我混淆了 JAX-RS 和 Spring MVC?
    • 查看你的导入包。 JAX-RS 有java.ws.rs,Spring 有spring。您不能只问在 JAX-RS 中执行此操作的正确方法是什么。您实际上需要设置和配置整个应用程序以运行 JAX-RS。然后,您可以浏览任何教程并查看如何使用不同的注释(即@Path@Produces@Consumes 等)。但是现在,您的应用程序运行在 Spring MVC 框架上,因此您需要遵循其框架的工作方式。
    猜你喜欢
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多