在 JAX-RS 中将 JSON 解组为 Java POJO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31990540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unmarshal JSON to Java POJO in JAX-RS
提问by kelevra88
I am looking to get the key and value to each Json formatted call and use them as java objects such as String or Integer ,in a rest client i would enter { "Name":"HelloWorld" }
我希望获取每个 Json 格式调用的键和值,并将它们用作 java 对象,例如 String 或 Integer ,在休息客户端中,我会输入 { "Name":"HelloWorld" }
And i would get back the HelloWorld mapped to its Key so far ive seen examples but im just having trouble finding out what each tag does and how to parse the body to give the above results
我会取回映射到它的 Key 的 HelloWorld 到目前为止我看到的例子,但我只是无法找出每个标签的作用以及如何解析主体以给出上述结果
@POST
@Path("/SetFeeds")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@JsonCreator
public String setFeed(String jsonBody,@Context UriInfo uriInfo){
...Code to manipulate the body of the request
return response;
}
回答by Paul Samsotha
First thing you need to understand is how request body parsing is done. In JAX-RS parsing (or unmarshalling/deserializing/whatever) is done with MessageBodyReader
s. There are different readers that can handle different Content-Type. For instance if you have Content-Type application/octet-stream
, there is a reader that will unmarshal to byte[]
or File
or InputStream
. So the following would work out the box
您需要了解的第一件事是请求正文解析是如何完成的。在 JAX-RS 中,解析(或解组/反序列化/任何)是用MessageBodyReader
s完成的。有不同的阅读器可以处理不同的 Content-Type。例如,如果您有 Content-Type application/octet-stream
,则有一个读取器将解组为byte[]
orFile
或InputStream
。所以以下内容将解决这个问题
@Consumes("application/octet-stream")
public Response post(File file) {} // or `byte[]` or `InputStream`
That being said, JAX-RS implementations come with very basic readers for "easily convertible" format. For example, most requests can be converted to String
, so you get that free for most Content-Types, as you are with your current code.
话虽如此,JAX-RS 实现带有非常基本的“易于转换”格式的阅读器。例如,大多数请求都可以转换为String
,因此您可以免费获得大多数内容类型,就像您使用当前代码一样。
If we want some more complex data types, like your HelloWorld
for Content-Type application/json
, there is no standard reader for this. For this to work, we either need to create our own reader, or use a library that comes with a reader. Luckily, the most popular JSON framework in Java, Hymanson, has implemented a JAX-RS provider that has a reader and a writer (for serialization).
如果我们想要一些更复杂的数据类型,比如您的HelloWorld
for Content-Type application/json
,则没有标准的阅读器。为此,我们需要创建自己的阅读器,或者使用阅读器附带的库。幸运的是,Java 中最流行的 JSON 框架Hymanson已经实现了一个 JAX-RS 提供程序,它具有一个读取器和一个写入器(用于序列化)。
Now depending on what server/JAX-RS implementation you are using, different implementations create light wrappers around the core Hymanson JAX-RS module. If I knew the JAX-RS implementation you were using, I could recommend which wrapper to use, or you can forget the wrapper and just go with the basic Hymanson module, which is
现在,根据您使用的服务器/JAX-RS 实现,不同的实现会围绕核心 Hymanson JAX-RS 模块创建轻量级包装器。如果我知道您正在使用的 JAX-RS 实现,我可以推荐使用哪个包装器,或者您可以忘记包装器而只使用基本的 Hymanson 模块,即
<dependency>
<groupId>com.fasterxml.Hymanson.jaxrs</groupId>
<artifactId>Hymanson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
The above is a Maven dependency. If you are not using Maven, then basically you need to download all these jars.
以上是Maven依赖。如果您不使用 Maven,那么基本上您需要下载所有这些 jar。
You can find all of them here. Just search for them individually.
你可以在这里找到所有这些。只需单独搜索它们。
Then you need to register the provider. Again it depends on your JAX-RS implementation and how you are handling the configuration of your resource classes. I would need to see your application configuration (either web.xml or Java code) and maybe the server you are using to help with that. For the most part the HymansonJsonProvider
(which is the reader and writer) needs to be registered.
然后你需要注册提供者。同样,它取决于您的 JAX-RS 实现以及您如何处理资源类的配置。我需要查看您的应用程序配置(web.xml 或 Java 代码)以及您用来帮助解决此问题的服务器。大多数情况下HymansonJsonProvider
(即读者和作者)需要注册。
Once you have it registered then you need to understand the basics of how Hymanson handle the serializaion. At most basiclevel, Hymanson looks for JavaBean properties(basic getter/setter) to match with JSON properties. For instance if you have this bean property
一旦你注册了它,那么你需要了解Hyman逊如何处理序列化的基础知识。在最基本的层面上,Hymanson 寻找与 JSON 属性匹配的JavaBean 属性(基本的 getter/setter)。例如,如果你有这个 bean 属性
public class HelloWorld {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The JSON should look like {"name": "whatever"}
. The "name"
key is the same as the bean property. In Bean property terms, the name of the property is all letters after the get/set
with the first letter lowercased.
JSON 应该看起来像{"name": "whatever"}
. 该"name"
键是一样的bean属性。在 Bean 属性术语中,属性的名称是 之后的所有字母get/set
,首字母小写。
That's pretty much all there is to it. Now you can do
这就是它的全部内容。现在你可以做
@Consumes("application/json")
public Response post(HelloWorld helloWorld) {
String name = helloWorld.getName(); // should == "whatever"
return Response.ok(helloWorld).build(); // we can also return objects
}
For more complex JSON formats, you should refer to the Hymanson documentation or ask a question here on SO.
对于更复杂的 JSON 格式,您应该参考 Hymanson 文档或在此处提出有关 SO 的问题。
As far as the registering of the HymansonJsonProvider
, if you are having trouble, please provide the information I requested, i.e. application configuration (web.xml or Java config) and the server you are using.
至于注册HymansonJsonProvider
,如果遇到问题,请提供我要求的信息,即应用程序配置(web.xml 或Java config)和您使用的服务器。
See Also:
也可以看看:
- JAX-RS Entity Providerto learn more about readers and writers
- JAX-RS 实体提供程序以了解有关读者和作者的更多信息