About Question enthuware.ocejws.v6.2.191 :

Moderators: Site Manager, fjwalraven

Post Reply
himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

About Question enthuware.ocejws.v6.2.191 :

Post by himaiMinh »

If the client wants to use JSON as an input and JSON as an output, we can still add MessageBodyReader and MessageBodyWriter.
For example, here is the psuedo code:

Code: Select all

@Provider
@Consumes(MEDIA_TYPE.JSON)
public class JSONMessageBodyReader implements MessageBodyReader<MathTable>{
....
public MathTable readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
                          MultivaluedMap httpHeaders, InputStream is) throws IOException, WebApplicationException
   {
      ObjectInputStream ois = new ObjectInputStream(is);
       //do some more steps to convert the input JSON string in the input stream into a JSON object...
         return  (MathTable)ois.readObject();
      
   }
    ....
}
 

Code: Select all

  @Provider
  @Produce(MEDIA_TYPE.JSON)
public class JSONMessageBodyWriter implements MessageBodyWriter<MathTable>{
  public void writeTo(MathTable mathTable, Class<?> type, Type genericType, Annotation[] annotations,
                       MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream os) throws 
 IOException, WebApplicationException
   {
     //do some steps to convert a MathTable object into a JSON string and write it to an output stream...  
    ObjectOutputStream oos = new ObjectOutputStream(os);
      
      oos.writeObject(mathTable);
   }
So, we can also provide TextMessageBodyWriter and TextMessageBodyReader by adding the annotations @Consumes (MEDIA_TYPE.PLAIN_TEXT) and @Produce(MEDIA_TYPE.PLAIN_TEXT).

Since we can provide the customized MessageBodyWriter and MessageBodyReader, the answer can be "all of the above".
Any comments?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by fjwalraven »

This would mean the question-maker would withhold you from important information. That would not be a well designed question.

Regards,
Frits

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by himaiMinh »

So, if the exam question mentions that there is a MessageBodyReader and MessageBodyWriter, we can consider any Java types of input and output is possible.

For example, the standard entity provider does not provide any JSON parsing in Jersey JAX-RS framework.
So, it won't work if there are no customized MessageBodyReader and MessageBodyWriter provided.

Code: Select all

   //The standard entity provider does not provide any MessageBody Writer to serialize the Person
 // object into a JSON string or a JSON object
   @Produce("application/json")
   public Person getPerson(@PathParam ("ID") String id){ ....}
  
If the exam question mentions that there are two providers that can serialize and deserialize any Java type object or primitive type into a corresponding output media type, then the above code will work.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by fjwalraven »

Yes, but note that Jersey by default supports JSON and XML. (important for the exam)

From the Jersey user guide
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T> providers
Regards,
Frits

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by himaiMinh »

I see this quote from Chapter 8 of Jersey API:
Jersey provides out-of-the-box support for JSONP - JSON with padding...MessageBodyWriter<T> for application/json media type, which also accepts the return type of the resource method, needs to be registered (see JSON section of this chapter).
But why the default standard entity provider in Jersey does not include JSON although application/json is supported?
Here is the list from Chapter 7 of Jersey:
byte[] (*/*)
String (*/*)
InputStream (*/*)
Reader (*/*)
File (*/*)
DataSource (*/*)
Source (text/xml, application/xml and media types of the form application/*+xml)
JAXBElement (text/xml, application/xml and media types of the form application/*+xml)
MultivaluedMap<K,V> (application/x-www-form-urlencoded)
Form (application/x-www-form-urlencoded)
StreamingOutput ((*/*)) - this class can be used as an lightweight MessageBodyWriter<T> that can be returned from a resource method
Boolean, Character and Number (text/plain) - corresponding primitive types supported via boxing/unboxing conversion
What tell me is if we have this:

Code: Select all

@GET
public byte[] getBytes(){ ...}
This will have @Produce("*/*") by default, or for another example,

Code: Select all

@GET
public Form getCredentials(@FormParam("username") String username) {....}
This will have @Produce(application/x-www-form-urlencoded) by default.

From the list, I don't see any application/json has any corresponding Java type.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by fjwalraven »

But why the default standard entity provider in Jersey does not include JSON although application/json is supported?
The standard entity providers are specified in the JAX-RS specs, which means they are required to be implemented. The Jersey implementation of the JAX-RS has got some extra entity providers (which I must say are quite handy).

If there is no @Produce annotation that means it will always have the default @Produce("*/*") annotation.

Regards,
Frits

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by himaiMinh »

Thanks for your reply.
I remember that I need to include jersey-json.jar, jackson-core.jar, jackson-jaxrs.jar, jackson-mapper.jar and etc in WEB-INF/lib folder when a REST service that consumes/produces JSON is deployed.

Those extra jar files are JSON parsers that are not included in Jersey's core API in "jaxrs-ri" folder. That is why I needed to download those jackson-*.jar and jersey-json.jar and etc to the WEB-INF/lib.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by himaiMinh »

To answer my own question, there is a JacksonJsonProvider that read/write object into JSON.
It is in org.codehaus.jackson.jaxrs.JacksonJsonProvider.java packaged in jackson-jaxrs-1.9.2.jar.
The Java code looks like this:

Code: Select all

@Provider
@Consumes({MediaType.APPLICATION_JSON, "text/json"})
@Produces({MediaType.APPLICATION_JSON, "text/json"})
public class JacksonJsonProvider implements MessageBodyReader<Object>, MessageBodyWriter<Object>,
   { ... }
This jar file should be included in the classpath.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by fjwalraven »

When you use Glassfish, you don't have to put any extra jar files to your classpath.

Regards,
Frits

witek_m
Posts: 16
Joined: Sat Jun 09, 2018 12:09 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by witek_m »

Hello, I`m not sure about JSON variant - in Mikalai Zaikin pdf we have "You can use JAXB annotations to map Java object to and from XML and JSON (no mentioned about special MessageBodyReader/Writer.)

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.191 :

Post by fjwalraven »

True, but in the question setup it is mentioned that the JAXB application class that is sent to and from the JAX-RS Web Service, and not JSON.

Regards,
Frits

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests