Reputation: 578
I am just starting to get into Spring and want to know if there are any example projects that expand on creating a rest service. I completed the rest-service tutorial (https://spring.io/guides/gs/rest-service/) and looked at a few others, but now have no clue how to incorporate hibernate into the mix.
As you may know, Spring 4.2 uses the new @RestController annotation which converts a model into JSON. All I am really looking for is an example which stays true to the tutorial I did plus a connection to hibernate, 1 database table, a model, service, and DAO. I don't need any html/jsp pages, so I don't think I need to go down the MVC route, but I could be wrong.
My thought is that I should be able to from the Controller do something like:
@RestController
public class GreetingController
{
@RequestMapping("/things")
public List<Thing> getThings()
{
ThingService service = new ThingService();
return service.getThings();
}
}
Where ThingService would call the dao, and the dao to the database.
Can anyone help? Linking to a good tutorial which does this, or taking the time to make me a super small example project would be greatly appreciated. Thank you in advance.
Upvotes: 2
Views: 2903
Reputation: 12734
Yes there is an easy way to implement your entities as a rest service or to call the dao in your controller and do some business logic.
First I suggest you to use Spring-Boot
. With Spring-Boot you are very fast in developing Spring applications. You are able to use the full stack of Spring and additional features. I think Spring-boot is exactly what you need.
For your Dao-Rest implementation is Spring-Data-Rest
the implementation you need. But first look up Spring-Data
for the initial informations.
Here is an example of Spring-Data and here a particular one for Spring-Data-Rest.
To start learning Spring using Spring-boot here is the starter if you dont use STS
IDE. More about Spring-boot you can lookup here.
Upvotes: 2
Reputation: 53
http://www.beingjavaguys.com/2014/08/spring-restful-web-services.html
This example would be helpful. Even though version is not Spring 4.2 but this example of Spring 4 and Hibernate 4 sufficient to satisfy your requirement. The versions can be modified in pom.xml and used.
another example with 3.2 spring framework.
Upvotes: 1