POST 的 REST 输入:相同的资源能否区分 JSON 和 XML 输入? [英] REST Input for POST: Can same resource differentiate between JSON and XML Input?

查看:46
本文介绍了POST 的 REST 输入:相同的资源能否区分 JSON 和 XML 输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相同的资源需要支持多种数据类型作为输入的要求?最好的方法是什么?我应该制作 2 个不同的资源并明确提及输入数据类型必须是什么吗?或者有没有办法仅仅根据像 Content-Type 这样的标题来区分输入对象?

I have requirement where a same resource needs to support multiple data types as input? What is the best way to do it? Should I make 2 different resource with explicitly mentioning what the input data type must be? Or is there a way to differentiate between the input object just based on a header like Content-Type?

另一个问题是,即使有一种方法可以在单个资源中做到这一点,我该如何定义我的对象?因为我正在考虑更改两种格式的对象.

Another problem is, even if there is a way to do it in a single resource, how do I define my object? As I was thinking of changing the objects for both the formats.

我正在使用 JAVA 进行开发.

I am using JAVA for development.

推荐答案

正如 Quentin 提到的,您可以使用 Content-Type 来区分输入资源.在 jax-rs 实现中,您可以使用 @Consumes 并在需要时使用 @Produces 注释来匹配请求并适当地处理它们.

As mentioned by Quentin you can use the Content-Type to differentiate the input resource. On the jax-rs implementation you can use @Consumes and if required @Produces annotations to match the requests and handle them appropriately.

如果您要为不同的输入格式绑定不同的对象,请使用不同的映射器.

If you are having different objects to bind to for the different input formats just use different mappers.

这篇关于POST 的 REST 输入:相同的资源能否区分 JSON 和 XML 输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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