I have an Angular Controller, a factory service and a app.js.
There is also a MongoDB in the background. So i have a GET Method and it's already running very well. But how do i create a post method over HTTP?
So there is a formular as a html file. There are some CheckBoxes and input fields. AngularJS i use for the Frontend, in the background theres a JAVA Program.
You can use the below shorthand syntax for GET and POST requests:
GET: $http.get('/URL').success(successCallback);
POST: $http.post('/URL', data).success(successCallback);
Refer to this link for details on how to set different parameters. You may also have a look at tutorials here.
Related
last time I found a example of REST app, which one REST controller returns HTML page(index.html). As a front-end was used Vue.JS and other communication was realized by REST controller returning normal JSON.
And now I stil thing how this solution looks in context of good practice of building REST API.
I add link to this controller. Home Controller
Ok. I read something and now I know that is a standard mechanism to serving view in Spring.
And now I see that HomeController is not Rest because have annotations #Controller not #RestController.
Thanks for help.
Of course NOT , Rest is use to send state information and not the UI
for UI we have other conrtoller to be used.
REST must be used when we need to send or recieve state information in form of json preferably.
Its a good programming paradigm to return JSON/XML as response to a rest call. This is separation of concern. Once the response is obtained from the Rest call we should process that to render an html object. Also this will ensure that if your rendering engine changes you will not have to change the code for the rest call. eg. You can change your view from struts to angular then your rest api code remains unchanged.
I am currently trying to build a RESTful API using raw JAX-RS. I have learned that when building REST APIs, there is the principle called HATEOAS(Hypermedia as the engine of application state). In my class we used Link Headers to tell the client, how to further progress the application. I have managed to implement all basic functionality and can access the server after deploying the application to a tomcat server.
My question now is, how do I add a header-link that contains a wildcard for the user to fill in, for example an id?
So far I have tried
#Path("/resources")
#Produces(MediaType.APPLICATION_JSON)
public Response listAllResources()
{
List<TestResource> resources = ...
// get stuff from database
return Response.ok(resources)
.link(UriInfo.getAbsolutePathBuilder().path("{id}").build(), "edit")
.build;
}
After I try to access the above defined path, I get an error message that the template variable id is undefined.
I can't find any helpful resource that shows me how to create a link header that looks like:
link: <http://example.com/api/resources/{id}>; rel: "edit"
I hope my question was clear enough since this is my first question on stackoverflow :)
Thanks in advance!
I found out, that links like in my example http://example.com/api/resources/{id} aren't possible by JAX-RS because the UriBuilder tries to resolve any URL part that's surrounded by curly braces. So just use like http://example.com/api/resources/:id, if you want to give an Uri Template. Unfortunately the client then has to do something like a String.replace() to actually "create" a valid URI.
I'm using the Stripes framework to develop a Java app.
I need to return my custom HTTP response codes, I mean, sometimes in an ActionBean I want to return something different to "200 OK" when I create or update some object.
I'm not able to find any documentation about this. Some help??
Many thanks.
as in
getContext().getResponse().setStatus(500);
I've written a rest interface (with jersey), a browser will be calling this rest interface. I would like show some html/jsp to the user as a response to this rest call...
Is this possible? How do I do it?
Yes, that is possible. This post as well as this one gives a hint how to use Viewables to return JSPs as a response.
I have a query form that I would like to submit as a GET request so the result page may be bookmarked and otherwise RESTful. It's your classical text field with a submit button. How do I induce Seam/JSF to use GET and include the query expression as a parameter rather than POST, the default?
All you need to do is enable the SeamFilter in web.xml. See Blog Example for an example RESTful application using Seam. The key is to use a Seam page parameter, defined in WEB-INF/pages.xml
you can use a PhaseListener to convert POST requests to GET requests or just to interpret GET requests so that they can be bookmarkable.
This page should explain in more detail:
http://balusc.blogspot.com/2007/03/post-redirect-get-pattern.html
If you are using s:button or s:link, your form will be using GET method.