简体   繁体   中英

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.

I could use @Pathparam or @queryparam based on what I wanted, but data doesnt like in either of those. 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
@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 . 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

@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. (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 . 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

@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. It is not difficult to make an InputStream into a String, a String parameter we will usually get support for free.

But what if we want unmarshal to a different type, say 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. For example, Jackson (I'd say the de facto JSON processor in Java) has a reader for us. 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. Then we will be able to use POJOs from our JSON. (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)

Some Resources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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