You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@juneau.apache.org by Steve Blackmon <sb...@apache.org> on 2018/08/05 15:40:20 UTC

Expanding supported MediaTypes

 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

Re: Expanding supported MediaTypes

Posted by James Bognar <ja...@apache.org>.
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 <sb...@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
>