Hi Jan
On 19/11/12 17:50, janb wrote:
Hi Sergey,
Thank you for your reply!
'to' method from WebClient seems to be the best fit for my use-case. But it is
still quite some code for this basic operation (fetching a resource that has
just been created). I would very much prefer if CXF could provide a more
convenient method to do hide the complexity here. Should I create a JIRA ticket
for this enhancement request?
My code now looks like this:
Response response = roleService.create(roleTO, null);
assertNotNull(response);
assertEquals(org.apache.http.HttpStatus.SC_CREATED, response.getStatus());
WebClient webClient = WebClient.fromClient(WebClient.client(roleService));
webClient.to(response.getLocation().toString(), false);
RoleTO actual = webClient.get(RoleTO.class);
As far as I know it is also quite acceptable to echo back the created
resource representation, alongside 201 & Location, see for example:
http://bitworking.org/projects/atom/rfc5023.html#create-example
I'm not 100% sure though if it os generally acceptable, but it can save
one the extra fetch operation.
Assuming we need to follow Location, what you can do is to enable
auto-redirect, example,
WebClient.getConfig(myClient).getHttpConduit().getClient().setAutoRedirect(true);
// effectively two requests are done:
RoleTo roleTo = roleService.post(new RoleTo(), RoleTo.class);
or we can introduce a specific utility method,
WebClient.postAndGet():
RoleTo roleTo = roleService.createAndGet(new RoleTo(), RoleTo.class);
given that it is a well-known pattern - however I'm hesitant - this will
require roleService to 'move' to to the Location URI internally...
Sergey
Best regards.
Jan
From: Sergey Beryozkin-5 [via CXF]
[mailto:[email protected]]
Sent: Montag, 19. November 2012 17:10
To: Jan Bernhardt
Subject: Re: Best way to handle JAX-RS Creat Resource URLs
Hi Jan
On 19/11/12 14:16, janb wrote:
Hi,
I'm currently wondering of what would be the best (CXF) way to handle a
response URL that I get, after saving a new resource.
In REST it is best practice to return Status Code 201 and a URL pointing to
the new resource after a POST request containing any kind of resource that
should be stored. My service I working well using JAX-RS annotations and CXF
as a Webservice-Stack. Now I would like to test my service. Therefore I
create a new resource receive the URL for this new resource back and would
like to GET this resource for purposes of unit testing.
Here is what I did:
Response response = roleService.create(roleTO, null);
assertNotNull(response);
assertEquals(org.apache.http.HttpStatus.SC_CREATED, response.getStatus());
Client client = WebClient.client(roleService);
WebClient webClient = WebClient.fromClient(client);
int offset = webClient.getBaseURI().toString().length();
webClient.replacePath(response.getLocation().toString().substring(offset));
RoleTO actual = webClient.get(RoleTO.class);
This code does not look nice to me. Since this should be a common usecase,
I'm wondering if there is a better way to do this?
I would like to reuse the configuration of my proxy object, to reuse my
provider and authentication configuration.
Can you clarify please what the code above does ?
Suppose we have "http://bar/roles"
and we have the role service returning
"http://bar/roles/1".
One option is indeed do
WebClient roleToService = WebClient.fromClient(roleService);
this will copy the actual original properties
and then do
// false flag will let use locations pointing to URIs not starting from
the base URI
roleToService.to(response.getLocation(), true);
This should do it.
Alterntaively you can 'browse' with the the original roleService, though
in this case you'll need to do some calculations:
String currentURILen = roleService.getCurrentUri().toString().length();
Response response = roleService.create(roleTO, null);
// advance
roleService.path(response.getLocation().substring(currentURILen));
// test new resource
// back to the URI pointing to "http://bar/roles"
roleService.back();
HTH, Sergey
Best regards
Jan
--
View this message in context:
http://cxf.547215.n5.nabble.com/Best-way-to-handle-JAX-RS-Creat-Resource-URLs-tp5718738.html
Sent from the cxf-user mailing list archive at Nabble.com.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
________________________________
If you reply to this email, your message will be added to the discussion below:
http://cxf.547215.n5.nabble.com/Best-way-to-handle-JAX-RS-Creat-Resource-URLs-tp5718738p5718744.html
To unsubscribe from Best way to handle JAX-RS Creat Resource URLs, click
here<http://cxf.547215.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5718738&code=amJlcm5oYXJkdEB0YWxlbmQuY29tfDU3MTg3Mzh8LTEzMDQ4ODk1MjM=>.
NAML<http://cxf.547215.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
--
View this message in context:
http://cxf.547215.n5.nabble.com/Best-way-to-handle-JAX-RS-Creat-Resource-URLs-tp5718738p5718769.html
Sent from the cxf-user mailing list archive at Nabble.com.