Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35753 > unrolled thread

Rest parameter without value

Started byJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
First post2016-10-14 15:28 +0300
Last post2016-10-17 08:30 +0300
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Rest parameter without value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-10-14 15:28 +0300
    Re: Rest parameter without value Arne Vajhøj <arne@vajhoej.dk> - 2016-10-14 14:49 -0400
      Re: Rest parameter without value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-10-17 08:48 +0300
        Re: Rest parameter without value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-10-17 09:50 +0300
    Re: Rest parameter without value 4ndre4 <626401fd-4bc7-4f8f-b641-563ae0f72920@626401fd-4bc7-4f8f-b641-563ae0f72920.invalid> - 2016-10-14 21:16 +0100
    Re: Rest parameter without value Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2016-10-17 08:30 +0300

#35753 — Rest parameter without value

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2016-10-14 15:28 +0300
SubjectRest parameter without value
Message-ID<m3lgxr8681.fsf@saunalahti.fi>
I have been working with java, but REST is new to me.
Now I am working on a REST client for the first time.
I have already managed to use a rest request with a path parameter and
another one with query parameters.

Now the request with a path parameter should in some occasions contain a
parameter without a value. 

I have used an url like this:
http://machine/path/v3.0/keyvalue
(where "keyvalue" is the variable path parameter and the url is
otherwise constant)

and in another context I should use
http://machine/path/v3.0/keyvalue?foo

Where "foo" is a constant parameter name.
But I haven't figured out how to add a parameter without a value!


What I have tried looks essentially like this:

String url = "http://machine/path/v3.0/{keyvalue}";
Private javax.ws.rs.Client client; // initialized in the constructor
.. 

private Object makeCall(String keyvalue, boolean b) {
  UriBuilder builder = UriBuilder.fromUri( url ).path( path ).
                        resolveTemplate( "keyvalue", keyvalue );
  WebTarget target = client.target( builder );
  if (b) {
    target = target.queryParam("foo", "");
  }
  Response resp = target.request().headers(makeHeaders()).get();
  Object ret = null;
  ..
  // resp handling not shown here
  ..
  return ret;
}

Debugging this, I lookd at target after the target.queryParam call and
noticed that target contains a ResteasyUriBuilder object uriBuilder,
where the attribute "query" now contains "foo=".
So this apparently will make the actual url look like this:

http://machine/path/v3.0/keyvalue?foo=

But I need it without the equals at the end. How do I do that?

I also tried passing null to the target.queryParam call instead of the
empty String,
  target = target.queryParam("foo", null);
but that way target.uriBuildeer.query remains null.

-- 
Jukka Lahtinen

[toc] | [next] | [standalone]


#35755

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-10-14 14:49 -0400
Message-ID<ntr9b4$1v2r$1@gioia.aioe.org>
In reply to#35753
On 10/14/2016 8:28 AM, Jukka Lahtinen wrote:
> I have been working with java, but REST is new to me.
> Now I am working on a REST client for the first time.
> I have already managed to use a rest request with a path parameter and
> another one with query parameters.
>
> Now the request with a path parameter should in some occasions contain a
> parameter without a value.

> What I have tried looks essentially like this:
>
> String url = "http://machine/path/v3.0/{keyvalue}";
> Private javax.ws.rs.Client client; // initialized in the constructor
> ..
>
> private Object makeCall(String keyvalue, boolean b) {
>   UriBuilder builder = UriBuilder.fromUri( url ).path( path ).
>                         resolveTemplate( "keyvalue", keyvalue );
>   WebTarget target = client.target( builder );
>   if (b) {
>     target = target.queryParam("foo", "");
>   }
>   Response resp = target.request().headers(makeHeaders()).get();
>   Object ret = null;
>   ..
>   // resp handling not shown here
>   ..
>   return ret;
> }
>
> Debugging this, I lookd at target after the target.queryParam call and
> noticed that target contains a ResteasyUriBuilder object uriBuilder,
> where the attribute "query" now contains "foo=".
> So this apparently will make the actual url look like this:
>
> http://machine/path/v3.0/keyvalue?foo=
>
> But I need it without the equals at the end. How do I do that?
>
> I also tried passing null to the target.queryParam call instead of the
> empty String,
>   target = target.queryParam("foo", null);
> but that way target.uriBuildeer.query remains null.

The API may not support it, because it does not make much sense
in REST.

http://host/resource  ~=  SELECT * FROM resource

http://host/resource/value  ~=  SELECT * FROM resource WHERE id = value

http://host/resource?nam=val  ~=  SELECT * FROM resource WHERE nam=val

http://host/resource?nam  ~=  ????

But as fallback mechanism you can always drop the JAX-RS client classes
and revert to Java HttpURLCOnnection or Apache HttpClient with a JSON
library. That gives you full freedom to do whatever you want.

Arne



[toc] | [prev] | [next] | [standalone]


#35794

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2016-10-17 08:48 +0300
Message-ID<m3lgxnh6fn.fsf@saunalahti.fi>
In reply to#35755
Arne Vajhøj <arne@vajhoej.dk> writes:
> On 10/14/2016 8:28 AM, Jukka Lahtinen wrote:

>> Now the request with a path parameter should in some occasions contain a
>> parameter without a value.
>> What I have tried looks essentially like this:
>> String url = "http://machine/path/v3.0/{keyvalue}";

I guess I'll try if this works when I just use
url = "http://machine/path/v3.0/{keyvalue}?foo";
when the valueless parameter is needed.

> The API may not support it, because it does not make much sense
> in REST.

I believe you, but I don't control the server application, I just need
to be able to call it..

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#35795

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2016-10-17 09:50 +0300
Message-ID<m34m4ba2pq.fsf@saunalahti.fi>
In reply to#35794
Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> writes:
> Arne Vajhøj <arne@vajhoej.dk> writes:
>> On 10/14/2016 8:28 AM, Jukka Lahtinen wrote:

>>> Now the request with a path parameter should in some occasions contain a
>>> parameter without a value.
>>> What I have tried looks essentially like this:
>>> String url = "http://machine/path/v3.0/{keyvalue}";

> I guess I'll try if this works when I just use
> url = "http://machine/path/v3.0/{keyvalue}?foo";
> when the valueless parameter is needed.

When I tested this against the real server application, it turned out
not to work as I expected.
I got it working when I used something like this:

        UriBuilder builder = UriBuilder.fromUri( url ).path( path ).
                        resolveTemplate( "keyvalue", keyvalue );
        WebTarget target = client.target( builder );
        if (foo) {
            target = target.queryParam( "foo", Boolean.TRUE);
        }
        Response resp = target.request().headers( buildHeaders()).get();

In other words, the QueryParam wasn't (or at least didn't need to be)
valueless after all, it worked with a Boolean value.
This may be a bug in the documentation of the server application.

In this case, the parameter that looked valueless isn't a key value, but
a switch that controls what the server application does if it doesn't
find the data in its database.

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#35759

From4ndre4 <626401fd-4bc7-4f8f-b641-563ae0f72920@626401fd-4bc7-4f8f-b641-563ae0f72920.invalid>
Date2016-10-14 21:16 +0100
Message-ID<ntref6$574$7@dont-email.me>
In reply to#35753
On 14/10/2016 13:28, Jukka Lahtinen wrote:

[...]
> I have been working with java, but REST is new to me.
[...]
> I have used an url like this:
> http://machine/path/v3.0/keyvalue
> (where "keyvalue" is the variable path parameter and the url is
> otherwise constant)

It does not really make sense. REST is based on the concept of 
"resource". Typically, you have something like:

http://somehost/somepath/users (a GET returns the list of users, a POST 
create a new user)

http://somehost/somepath/user/user-id (a GET returns the user, a PUT 
updates the user)

etc..

Another important concept is HATEOAS.

I suggest that you read up about correct REST API construction. There's 
plenty of resources around. For example, the following links:

http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#restful

http://restful-api-design.readthedocs.io/en/latest/resources.html

-- 
4ndre4

[toc] | [prev] | [next] | [standalone]


#35793

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2016-10-17 08:30 +0300
Message-ID<m3pomzh7a5.fsf@saunalahti.fi>
In reply to#35753
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> writes:
>>I also tried passing null to the target.queryParam call instead of the
>>empty String,
>>  target = target.queryParam

>   So, instead of
> target = target.queryParam( "example", "" );
>   , what does
> target = target.path( "?example" );
>   give?

That would add a '/' before the "?example". Not what I need.

-- 
Jukka Lahtinen

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web