CRUD on Spring Data Rest sub-resources using AngularJS? - angularjs

I'm using a Spring Data REST backend with AngularJs as frontend.
How is the best way to add sub-resources to a "root Entity"?
From official documentation section 4.4.1:
http://docs.spring.io/spring-data/rest/docs/2.3.0.RELEASE/reference/html/#repository-resources.search-resource
we can POST to association resource using "text/uri-list"
A tipical example of sub-resource are Comments to a BlogPost like this question:
POSTing a #OneToMany sub-resource association in Spring Data REST
From Spring documentation I should proceed in two steps:
add the new sub-resource (POST a new Comment)
and then add the link (POST a text/uri-list to the BlogPost comments)

I don't see the relation with AngularJS : you can either use $http or $resource or halClient to POST your entities.
If you have a bi-directionnal relation between blogpost and comment, it should work just by POSTing the comments with the 'parentBlogPost' field filled. If you get back the blogpost, its comments would be updated automatically.
If you have uni-directionnal relation, then you need to POST the uri-list too.

Related

Spring MVC with AngularJS for presentation (is REST necessary ?)

I plan to use Spring MVC for a JEE application and i'm still debating whether to use AngularJS or PrimeFaces for presentation. My question is :
I've done some research concerning AngularJS with Spring MVC and i have found pretty much everyone talking about using REST API in Spring to make the connection with AngularJS pages(?). Is that the only way to use these two technologies together ?
In order to make the spring controller/methods to be used as REST API, all you need to do is, annotate the controller with #RestController annotation.
Also in order to make the Java object to JSON and vice versa you can use Jackson API. Adding this snippet in your XML file and relevant dependency/jar in project will help you transfer data back and forth easily.
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
No, not necessary. Your angular frontend will be expecting JSON data to render. As far as you can call your service be it any api and it (api) can return JSON it is good.
but rest is a preferred option for using angular as it is simpler to be configured.

How to consume Spring Data Rest Webservices API with AngularJS

i had been working in a Spring Rest Data API in an application (just for fun).
I have the core made, works fine but now I want to use AngularJS as front-end.
I had work with Jackson (Mapping Java Objects to JSON), but with Spring Rest Data the Json response it's diferent, it has _embedded, _links, self, etc. links that make me confuse. I have something like this in the root url http://localhost/8080/app/api/tarifas
I have used a JS script called restangular but i have serveral problems (I'm newbie with Angular)
In my controller i have this
Check the error
If I add a RestangularProvider in my app.config(...) and change the Controller to getList instead get, works fine, but I need several entities data formats.
Any help it's welcome. If you know a better way please tell me.
Thanks!!
UPDATE
I found a form to do this (i donĀ“t know if it's the better) but now my problem is the next:
I have objects that has other objects as attributes (realtionship), and the reference in the JSON is a link (not an object). Then, in the grid; the value of the description's internal object is blank. To get the json data I found this
Now I have the next content for one register (one of the grid)
And my grid (In Angular, HTML) looks like this (empty fields)
How can I retrieve the attribute description from the member estado, categoria, etc. and show it in the grid. Should make the request to get it?
Thanks!!
You can use either angular.toJson or json.stringify
Angular-toJson documentation
Json.stringify documentation

Module circular dependency due to using the same resource

I'm using parse.com as my backend. I have 2 tables: post and comment. A post have multiple comments, so it is a one to many relationship. so I have comment point to the post (which is easy) but also an array on post that list all the comments this post have. I imagine if I used MongoDB or any other NoSQL it will be the same.
On front end, I have 2 modules: "post" and "comment". Each have a resource that is hooked with the backend. Each module also have all the CRUD pages that create, read, update and delete. When I write the view part, a post need to display all the comments below, so my post module depends on comment module and will use comment resource to find all comments for this post.
But when I write the create part for comment, I found myself need to update the post array and add the newly created comment to the array. Which means I need the post resource to update the post. The post resource is in the post module so now my comment module depends on post module and created a circular dependency.
I have 2 solutions I'm going to share, but I want to know if anyone have anything better:
I use angular-ui-router in my project. After a comment is created, I route back to a post state called "app.post.commentCreated" . This state is handled inside the post module. It will update the post array of comments.
I separate the post resource and comment resource to their own module called "postResource" and "commentResource" and make post and comment module depends on both. I have not done this yet. I'm about to.
Do you have anything better?
Thank you very much!

How to use AngularJS with Struts 1.x

I'm new to AngularJS and client side stuff and I'm adding a new page to an old application that uses Struts 1.3 as its framework. I would like to use AngularJS for the front end.
I know how to return JSON from the action class by writing the JSON to the front end and returning null for the action forward. However, I'm unclear how I would populate the scope variables within the controller after the GET. If I use a GET in the controller and get JSON back, how does my ActionForm fit into all this? Is it useful at all? Can I have a GET and POST for the same controller if I want to send new values back to my action? And, can I have variables like:
$scope.items={}
$scope.items.name=""
$scope.items.email=""
And then just send json.stringify(items) as my data in the POST if I can't use the form somehow?
I haven't found much information using AngularJS with Struts 1.3 so far.
EDIT:
I'll try to answer as best as possible what conclusion I came to but my questions above were very vague since I didn't really know what I was talking about and my position with the company I worked for has ended so I no longer have access to that code. What I meant to ask earlier was what am I going to do with the action form that I usually use for Struts actions and how am I going to get data from the front end to my action class during a POST. I found out that my usual ActionfForm was useless for what I wanted to do so I got rid of it and wrote a JSON object during the GET that would be modified on the frontend and passed back to another action when I did the POST. This is done like a normal POST to whateverAction.do, but I had to configure the data I was sending in the POST and name it something. I then picked up the modified JSON object by using
request.getParameter("jsonObjName")
and parsed it to different LinkedHashMap objects for each object in the JSON object. I think you can use the JSONObject classes instead of LinkedHashMap to parse if you are using JavaEE but I was using SE so I didn't have access to those in this project. Here a link to another page I used for the POST configurations:
How to get the data in Struts from AngularJS post
On the Java side you can also try to use Gson library that allows you to parse string to and from JSON, but Struts 1 generally won't fit well with AngularJS, if you remove the form on Java side, than makes no sense to keep Struts 1.
And if no possibility to remove Struts 1, than just keep Struts 1 for existing functionality and start using Struts 2 for new screens where AngularJS is applied.
It will be much easier cause the JSON from AngularJS' POST will automatically be bound to the bean object that has to match to JSON properties format and name.

Cakephp twitter autopost

I need to post a product details in twitter whenever it is added in my website.I am using Cakephp, and i use twitter datasource available in book.cakephp.org . It returns a 'Basic authentication is not supported ' message
when i searched it, i get that twitter is now using OAuth, how can i impliment this in my data source?. i did it referring the below url
http://book.cakephp.org/view/1077/An-Example
Anybody please help.
The Cake Book link seems to be out of date and relevant to the time before Twitter implemented OAuth.
I have accomplished what you're trying to do using http://code.42dh.com/oauth/ but this is basically a controller and a component, not a datasource as you asked.
There's a CakePHP OAuth Datasource for CakePHP which might work (I haven't tried it) but you'll need to add the PHP Oauth library too. If you choose to do this, let me know if it works as interacting with Twitter as a datasource makes a lot more sense, especially as Cake are depreciating components.
I have created this - it isnt a datasource but it provides a model you can use to accomplish what you need to do:
https://github.com/voycey/CakePHP-Twitter-API-v1.1-Full
You can either use a Shell to have it post automatically or you can just call it in your controller when you add your entity.

Resources