juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Bognar <jamesbog...@apache.org>
Subject Re: How to get a class to return standard JSON as the default
Date Sat, 10 Feb 2018 19:03:22 GMT
I uploaded a change to 7.1.0-SNAPSHOT that should make this work better.

The problem was that given "Accept: */*", the media type
"application/json+simple" was considered a slightly better match than
"application/json" (i.e. two subtype parts match instead of just one).  So
that why the ordering didn't matter.  I've adjusted the algorithm to remove
that 'feature'.  :-)

You should be able to place JsonSerializer.class at the top of the
serializers now and have it match against "*/*".

One caveat is I believe if you place JsonSerializer.class at the top of the
list, you'll always see JSON rendered on Internet Explorer (unless they've
fixed their Accept header in later releases...I no longer have a Windows
machine to test against).

On Fri, Feb 9, 2018 at 2:21 PM, James Bognar <jamesbognar@gmail.com> wrote:

> Yea, unfortunately it's not that simple.  I'm still investigating though.
>
> On Fri, Feb 9, 2018 at 2:15 PM, Gary Gregory <garydgregory@gmail.com>
> wrote:
>
>> On Fri, Feb 9, 2018 at 11:06 AM, James Bognar <jamesbognar@apache.org>
>> wrote:
>>
>>> I'll have to look further on how to support that.  It's kind of a
>>> can-of-worms.
>>>
>>> We can't simply go by order of the serializers because HtmlDocSerializer
>>> is listed first because some versions of IE do not specify "text/html" in
>>> their Accept header.
>>>
>>> This is the Accept header for Internet Explorer:
>>> image/jpeg, application/x-ms-application, image/gif,
>>> application/xaml+xml, image/pjpeg, application/x-ms-xbap,
>>> application/x-shockwave-flash, application/msword, */*
>>>
>>> And the heuristics for matching are complex.  For example, the Accept
>>> header could contain id properties (e.g. "application/activity+json",
>>>
>>> https://www.w3.org/TR/activitypub/#retrieving-objects).
>>>
>>>
>>> Examples:
>>>
>>>    - "Accept: application/json" should match "application/json" before
>>>    "application/json+simple".
>>>    - "Accept: application/json+simple" should match
>>>    "application/json+simple" before "application/json".
>>>    - "Accept application/json+activity" should match "application/json"
>>>    before "application/json+simple".
>>>    - "Accept: */json" should match  "application/json" before
>>>    "application/json+simple".
>>>    - "Accept: application/*" should match ???
>>>
>>> Well, this is a nasty pickle.
>>
>> If I add my own "serializers" and put JSON first:
>>
>>                         serializers={
>>                                 JsonSerializer.class,
>>                                 JsonSerializer.Simple.class,
>>                                 JsonSchemaSerializer.class,
>>                                 HtmlDocSerializer.class,
>>                                 HtmlStrippedDocSerializer.class,
>>                                 HtmlSchemaDocSerializer.class,
>>                                 XmlDocSerializer.class,
>>                                 XmlSchemaDocSerializer.class,
>>                                 UonSerializer.class,
>>                                 UrlEncodingSerializer.class,
>>                                 MsgPackSerializer.class,
>>                                 SoapXmlSerializer.class,
>>                                 PlainTextSerializer.class
>>                         },
>>
>> And and let Postman use Accept */*,I still get JSON with single quotes.
>> Is there no way to get JSON with double quotes?
>>
>> Gary
>>
>>
>>> On Fri, Feb 9, 2018 at 12:43 PM, Gary Gregory <garydgregory@gmail.com>
>>> wrote:
>>>
>>>>
>>>>
>>>> On Fri, Feb 9, 2018 at 9:40 AM, James Bognar <jamesbognar@apache.org>
>>>> wrote:
>>>>
>>>>> Just tried Postman....it too specifies "Accept: */*" as a default
>>>>> value.  If you specify Accept as a single space, it appears to work.
>>>>>
>>>>
>>>> Hi James,
>>>>
>>>> I think I am getting closer. How do I get "*/*" to map to
>>>> "application/json" in @RestResource as opposed the the HTML table I
>>>> get now?
>>>>
>>>> Gary
>>>>
>>>>
>>>>> If you add ?debug=true to your URL, the request will get logged to the
>>>>> console so that you can see the actual incoming request:
>>>>>
>>>>>  [2018.02.09 11:39:22 WARNING]
>>>>>
>>>>> === HTTP Request (incoming) ==============================
>>>>> ======================
>>>>>
>>>>> HTTP GET /?debug=true
>>>>>
>>>>> ---Headers---
>>>>>
>>>>> Cookie: JSESSIONID=node01siuixuu8fdsh1hm4a6dqe3lyg2.node0
>>>>>
>>>>> Cache-Control: no-cache
>>>>>
>>>>> Accept: */*
>>>>>
>>>>> Connection: keep-alive
>>>>>
>>>>> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2)
>>>>> AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36
>>>>>
>>>>> Host: localhost:10000
>>>>>
>>>>> Postman-Token: 2b9124b3-1393-0e96-b052-edac7be1e8dd
>>>>>
>>>>> Accept-Encoding: gzip, deflate, br
>>>>>
>>>>> Accept-Language: en-US,en;q=0.9
>>>>>
>>>>> ---Default Servlet Headers---
>>>>>
>>>>> accept: application/json
>>>>>
>>>>>
>>>>> === END ============================================================
>>>>> ============
>>>>>
>>>>>
>>>>> On Fri, Feb 9, 2018 at 11:25 AM, Gary Gregory <garydgregory@gmail.com>
>>>>> wrote:
>>>>>
>>>>>> On Fri, Feb 9, 2018 at 7:51 AM, James Bognar <jamesbognar@gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> Hi Gary,
>>>>>>>
>>>>>>> Try using a default request header:
>>>>>>>
>>>>>>> @RestResource(
>>>>>>>
>>>>>>> defaultRequestHeaders= {
>>>>>>>
>>>>>>> "Accept: application/json"
>>>>>>>
>>>>>>> },
>>>>>>>
>>>>>>> )
>>>>>>>
>>>>>>> I'll take a closer look at how "no" Accept header is handled.
 I
>>>>>>> think it depends on what tool you're using.  Poster for example
sends
>>>>>>> "Accept: */*" if you don't specify a value, and that might return
a
>>>>>>> somewhat-random result.
>>>>>>>
>>>>>>
>>>>>> That does not work for me sadly. Here a snipet of my service that
extends
>>>>>> ResourceGroup. I am using Postman 5.5.2. I am getting back an HTML
table
>>>>>> for my service.
>>>>>>
>>>>>> @RestResource(
>>>>>> // @formatter:off
>>>>>> path="/",
>>>>>> title="Access & Connectivity Agent Microservice",
>>>>>> description="Administration",
>>>>>>         defaultRequestHeaders= {
>>>>>>                 "Accept: application/json"
>>>>>> },
>>>>>> htmldoc=@HtmlDoc(
>>>>>>                 head={
>>>>>>                         "<link rel='apple-touch-icon' sizes='57x57'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-57x57.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='60x60'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-60x60.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='72x72'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-72x72.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='76x76'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-76x76.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='114x114'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-114x114.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='120x120'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-120x120.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='144x144'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-144x144.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='152x152'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-152x152.png}'/>",
>>>>>>                         "<link rel='apple-touch-icon' sizes='180x180'
>>>>>> href='$U{servlet:/htdocs/images/apple-icon-180x180.png}'/>",
>>>>>>                         "<link rel='icon' type='image/png'
>>>>>> sizes='192x192'  href='$U{servlet:/htdocs/image
>>>>>> s/android-icon-192x192.png}'/>",
>>>>>>                         "<link rel='icon' type='image/png'
>>>>>> sizes='32x32' href='$U{servlet:/htdocs/images/favicon-32x32.png}'/>",
>>>>>>                         "<link rel='icon' type='image/png'
>>>>>> sizes='96x96' href='$U{servlet:/htdocs/images/favicon-96x96.png}'/>",
>>>>>>                         "<link rel='icon' type='image/png'
>>>>>> sizes='16x16' href='$U{servlet:/htdocs/images/favicon-16x16.png}'/>",
>>>>>>                         "<link rel='manifest'
>>>>>> href='$U{servlet:/htdocs/images/manifest.json}'/>",
>>>>>>                         "<meta name='msapplication-TileColor'
>>>>>> content='#ffffff'/>",
>>>>>>                         "<meta name='msapplication-TileImage'
>>>>>> content='$U{servlet:/htdocs/images/ms-icon-144x144.png}'/>",
>>>>>>                         "<meta name='theme-color' content='#ffffff'/>"
>>>>>>                 },
>>>>>>                 header={
>>>>>>                 "<h1>$R{servletTitle}</h1>",
>>>>>>                 "<h2>$R{methodSummary,$R{servletDescription}}</h2>",
>>>>>>                 "<a href='https://www.rocketsoftware.com'><img
>>>>>> src='$U{servlet:/htdocs/images/ram.png}'
>>>>>> style='position:absolute;top:5;right:5;background-color:tran
>>>>>> sparent;height:30px'/></a>"
>>>>>>         },
>>>>>> widgets={
>>>>>> ContentTypeMenuItem.class,
>>>>>> StyleMenuItem.class,
>>>>>> },
>>>>>> navlinks={
>>>>>> "options: servlet:/?method=OPTIONS",
>>>>>>         "$W{ContentTypeMenuItem}"
>>>>>> }
>>>>>> ),
>>>>>> children={
>>>>>> ConfigResource.class,
>>>>>> LogsResource.class
>>>>>> }
>>>>>> // @formatter:on
>>>>>> )
>>>>>> public class AdminRootResources extends ResourceGroup {
>>>>>> ...
>>>>>>     @RestMethod(name = GET, path = PATH_STATUS, summary = "Status",
>>>>>> description = "Gets status for the running system.")
>>>>>>     public Map<String, Object> getStatus() {
>>>>>> ...
>>>>>>
>>>>>> Thank you,
>>>>>> Gary
>>>>>>
>>>>>>
>>>>>>>
>>>>>>> On Thu, Feb 8, 2018 at 9:11 PM, Gary Gregory <garydgregory@gmail.com
>>>>>>> > wrote:
>>>>>>>
>>>>>>>> Hi All:
>>>>>>>>
>>>>>>>> I want to return _standard_ JSON (see json.org) when no Accept
>>>>>>>> header is sent to my @RestResource class. I cannot seem to
get that to work
>>>>>>>> no matter what. I get Juneau's non-standard single quote
JSON.
>>>>>>>>
>>>>>>>> Standard JSON uses double quotes, not single quotes.
>>>>>>>>
>>>>>>>> If I specify 'application/json' in the Accept, then I do
get
>>>>>>>> standard JSON.
>>>>>>>>
>>>>>>>> I cannot get this to work no matter what I try in my class's
>>>>>>>> @RestResource's serializer attribute.
>>>>>>>>
>>>>>>>> I also want to support the normal set of serializers so users
can
>>>>>>>> specify any Accept header value Juneau supports.
>>>>>>>>
>>>>>>>> Thank you,
>>>>>>>> Gary
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>

Mime
View raw message