I'm trying to determine the correct API calls on FacesContext to do the following when processing a backing bean action:
Within the action method construct a URL of dynamic parameters
Send the constructed URL to service
Parse the returned service response parameters
Continue with action method processing based on reponse string from request.
Any suggestions on the highlevel API calls for steps 2 and 3 to send me in the right direction would be very appreicated. Note, the service I'm calling is external to this application in a blackbox. Instructions are: send URL in specified format, parse response to see what happened.
This problem is not specific to JSF in particular, so you'll find nothing in JSF API. The standard Java API offers java.net.URL or, which allows more fine grained control, java.net.URLConnection to fire HTTP requests and obtain the response as an InputStream which you can then freely parse the usual Java way.
InputStream response = new URL("http://google.com").openStream();
// ...
Depending on the content type of the response, there may be 3rd party API's which ease the parsing. For example, Google Gson if it's JSON or Jsoup if it's HTML/XML.
Related
I am looking for away to make “internal” rest calls from a service entry point into rest services that are declared in the same war file.
Currently, I am using http connection to localhost. However, I believe dispatching the request directly (with requestDispatcher ?) will be more efficient - no need for connection, no need for extra execution threads, no need to send data via tcp socket, etc.
When I need the “internal” call, I do not know what is the actual object that will represent the payload, or the class/method that will process the request. All I have is the url, and a json string for the payload. I expect the response to be a json string.
Is there a standard method that will work for all rest container (e.g. using the servlet api) or using specific functions of Jersey/spring ?
The Spring boot API could not parse the x-www-form-urlencoded type request since the client posted the data differently than what Java supports.
The incoming request body looks like below:
DeliveryPostCode=TEST123000&EnableCOD=false&Basket%5B0%5D%5BId%5D=383293820&Basket%5B0%5D%5BCategory%5D=Smartphone
In which a Basket is an object which has Id, Category as fields.
Decoding the above URL gives -
DeliveryPostCode=TEST123000&EnableCOD=false&Basket[0][Id]=383293820&Basket[0][Category]=Smartphone
which does not work for Java
If we can get a request which looks like below, It works for Java -
DeliveryPostCode=TEST123000&EnableCOD=false&Basket[0].Id=383293820&Basket[0].Category=Smartphone
Since the client is something which we cannot afford to change. We have to do some manipulation in Java before Controller takes this
OR
a modify in middleware node js layer we have between client & server which acts as a proxy.
Please suggest.
I am an aspiring Android Developer, I have faced many issues in integrating my REST apis in Android. Existing solutions like Retrofit, volley are very generic.
I have been working on creating a generic framework for REST apis. Need help to complete it. I have configured components as follows:
Assumptions: Json data type as request body and json data type as request response
CentralCommandClass (activities will access this class to perform network operations with callback method as parameter)
API class extends IntentService (static functions for get, post, put etc to start service with different parameters)
MyHttpRequest (having execute method to open url connection and gets response from server)
My questions are as follows:
Does this approach has any downside?
There is a requirement to fire few requests only after one specific request has been fired, how do I handle this case?
I am using Jerysey's implementation of JAX-RS to write a REST API. My POJO that handles the get requests eventually forwards it to a JSP. Everything is working fine, the problem is that the forwarding causes the URL in the browser's address bar to change to the URL that the request was forwarded to. How do I do a redirect WITHOUT this URL changing in the address bar? Current, I have tried 4 different ways:
return Response.seeOther(uri).build(),
return Response.temporaryRedirect(uri),
//thrown exception:
throw new WebApplicationException(response),
return Response.status(303).location(uri).build();
It doesn't sound like a Jersey issue per se. Jersey is doing its part to receive the request, do some processing, and return the response you are expecting.
It sounds more like a servlet container issue. Why don't you want the url to change in the browser?
Restful services can (and should) be built with no concern about templates/JSPs/consumers. Take a look at a library like RestAssured, write some tests for your work, and you will see that it is acting as expected.
Instead of rendering out to a JSP, consider using a rest client to make straight http requests against your service.
If you want the url to remain unchanged, consider making the http call using an AJAX library (JQuery or other Javascript-based solution).
I hope that helps!
A RESTful resource is identified by a URL. So if you redirect to another resource with another URL the URL in the address bar should change. That's good because you can e.g. bookmark this URL or send it per eMail.
The question here is are you really redirecting to another resource or do you only want to return a different representation (HTML instead of e.g. JSON). If the latter you should not redirect. Let your resource-class directly return text/html by using Jerseys Viewables.
You could make the entire website inside an iFrame and load the new site into that frame. It will maintain the page URL and load your content.
http://www.w3schools.com/tags/tag_iframe.asp
I am trying to implement the example shown here.
The example seems to be for a jersey setup, which I am not using or familiar. How hard would it be to convert this to a standard java servlet project(idk how to name this)
What steps should I take. It seems most of the # annotations need to be changed to servlets.
This also seems very differnt from the standard appengine upload setup which all takes place in one servlet.
This would be a lot of work to rework the code to standard servlet and remove jersey. Jersey takes away so much boilerplate code. For example the JSON conversion is done by jersey, which otherwise would have to be custom implemented.
And you can for sure deploy more than one servlet to gae, in which way should this be standard?
Just look at the first method:
#GET
#Path("/url")
public Response getCallbackUrl() {
String url = blobstoreService.createUploadUrl("/rest/file");
return Response.ok(new FileUrl(url)).build();
}
When using only standard servlet you would need to do:
Servlet Definition and Mapping in web.xml to /url
Implement a HttpServlet, override doGet() method
Send Response Code 200 OK
Set appropriate HTTP Response Headers
Convert Response to JSON and write it to response