简体   繁体   English

从JAX-RS中的帖子中检索数据

[英]retrieve data from post in JAX-RS

The server is sending data in JSON as HTTP POST, and I am using jax-rs to handle and retrieve data. 服务器正在使用JSON作为HTTP POST发送数据,而我正在使用jax-rs来处理和检索数据。

I could use @Pathparam or @queryparam based on what I wanted, but data doesnt like in either of those. 我可以根据自己的需要使用@Pathparam或@queryparam,但其中任何一个都不喜欢数据。 Not in header either, if I am right, as they content something like content-type, date and some similar sort. 如果我是对的,也不在标题中,因为它们的内容类似于内容类型,日期和类似的排序。 How do I retrieve data from POST? 如何从POST检索数据?

@POST
@Path("/foo")
public void foo(){ //do i need to put sth in parameter paranthesis to get?
    //handle the data!! but how??
}

I suggest you take a look at JAX-RS Entity Providers . 我建议您看一下JAX-RS Entity Providers I will explain to you how marshalling and unmarshalling is done to and from the response and request stream, respectively. 我将向您解释如何分别对响应流和请求流进行编组和反编组。

Basically you have MessageBodyReader s and MessageBodyWriter s (the former being the one the unmarshall from the request stream. How is works say you have a method like so 基本上,您有MessageBodyReaderMessageBodyWriter (前者是从请求流中解组的那个。工作原理表明您有这样一种方法

@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response postString(String s) {}

The String s , like any other method parameter without an annotation is treated as the body of the request. 像其他任何没有注释的方法参数一样, String s被视为请求的主体。 (Note a method can only have one non-annotated parameter; that is a request can only have one body). (请注意,一个方法只能有一个非注释参数;也就是说,一个请求只能有一个正文)。 So what happens is the JAX-RS (implementation's) runtime will look through the registry of providers ( MessageBodyReader s to be exact) to look for one that can handle unmarshalling a body of type text/plain into a String . 因此,发生的事情是JAX-RS(实现的)运行时将通过提供程序的注册表(确切地说是MessageBodyReader )进行查找,以寻找可以处理将text/plain类型的text/plain编组为String There are some standard readers for some standard types, and this is one that is available for free. 有一些用于某些标准类型的标准阅读器,这是免费提供的一种。

Now in the case of JSON, say we have this 现在以JSON为例,说我们有这个

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response postJson(String json) {}

This can be done also because there is a writer that can handle this. 之所以可以这样做,是因为有一个作家可以处理。 Basically a String parameters can be handled most of the time. 基本上,大多数时间都可以处理String参数。 It is not difficult to make an InputStream into a String, a String parameter we will usually get support for free. 将InputStream转换为String并不难,我们通常会免费获得String参数。

But what if we want unmarshal to a different type, say Foo 但是,如果我们想解组到不同类型的,说Foo

class Foo {
    String bar;
    String baz;
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response postJson(Foo foo) {}

And we had JSON like 我们有像

{ "bar" : "barValue", "baz": "bazValue" }

We can do this, but we need a custom reader. 我们可以做到,但是我们需要一个定制的阅读器。 Luckily there are some already out there. 幸运的是已经有一些了。 We just need to add the library dependency to out project. 我们只需要将库依赖项添加到out项目中即可。 For example, Jackson (I'd say the de facto JSON processor in Java) has a reader for us. 例如,杰克逊(我会说Java中的事实上的JSON处理器)为我们提供了一个阅读器。 It's in the dependency 在依赖中

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.5.0</version>
</dependency>

We can just register the JacksonJsonProvider into our application. 我们可以将JacksonJsonProvider注册到我们的应用程序中。 Then we will be able to use POJOs from our JSON. 然后,我们将能够使用JSON中的POJO。 (For more help on registering this, please provider more information about what JAX-RS implementation you are using, and show your application configuration, whether it's web.xml or Java config) (有关注册此功能的更多帮助,请提供有关您正在使用的JAX-RS实现的更多信息,并显示您的应用程序配置,无论是web.xml还是Java配置)

Some Resources: 一些资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM