juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Bognar <jamesbog...@apache.org>
Subject Re: Expanding supported MediaTypes
Date Sun, 05 Aug 2018 16:18:26 GMT
Hi Steve,

The MediaType objects are simply parsed strings.  To create a new one, you
can simply call MediaType.forString("application/stream+json");

I can think of two options if you want to specify different PojoSwaps
depending on the media type.

*1) Per-media-type PojoSwaps.*
For example:

@Swap(mediaTypes="application/json")
public class NormalSwap extends PojoSwap<MyBean,ObjectMap> {...}

@Swap(mediaTypes="application/stream+json")
public class StreamSwap extends PojoSwap<MyBean,ObjectMap> {...}

@Swaps(NormalSwap.class, BeanSwap.class)
public MyBean {...}

When multiple swaps match a media type, the best match wins.  So if the
media type is Accept type is"application/stream+json", then the 2nd swap
should get picked over the 1st.
http://juneau.apache.org/site/apidocs/overview-summary.html#juneau-marshall.PerMediaTypePojoSwaps

*2) Just override the swap method to return different values.*

The request media type is part of the bean session.  You can use this to
alter the behavior of your swap at runtime:

public class MySwap extends PojoSwap<MyBean,ObjectMap> {
   public ObjectMap swap(BeanSession session, MyBean b) {
      if (session.getMediaType().hasSubType("stream")) {
         ...
      } else {
         ...
      }
   }
}


On Sun, Aug 5, 2018 at 11:40 AM Steve Blackmon <sblackmon@apache.org> wrote:

> Hello,
>
> I’d like to write some PojoSwaps that convert Pojos from one package into
> corresponding Pojos from another package, conditional of a specific mime
> type being requested.
>
> However most fields in org.apache.juneau.http.MediaType are final and/or
> private, so I’m unsure how to proceed with adding MediaType for
> application/stream+json
>
> Any ideas?
>
> Thanks,
> Steve
>

Mime
View raw message