I'm new to Java and Spring but not programming. This is my first post and I read that this was a good place to help with Java
I'm looking to build a REST API and I was going through some Spring tutorials (https://spring.io/guides/gs/accessing-data-rest/ + https://spring.io/guides/gs/rest-service/).
I noted that the one uses the restful controller approach and the other uses the RepositoryRestResource and omits the controller. I guess I'm trying to find out which would be the better approach. To give more ideas on my API. I basically envisage it needing some authentication (likely OAuth) and authorization. eg authenticated to view x and authorized to edit x.
I'm looking for my API to then power a website and then iOS / Andoird if necessary.
On a related side note, I just wondered if there were any tools to generate JPA entity classes from an existing DB structure? I'm using intellij community
I hope that you find the community here welcoming, helpful, knowledgeable and sharing.
To answer your question about some of the differences between the two tutorials:
This tutorial https://spring.io/guides/gs/accessing-data-rest/ titled "Accessing JPA Data with REST" interacts with a database the h2database to be exact.
This is why it uses the @RepositoryRestResource notation.
Plus this tutorial uses the @SpringBootApplication notation
Tutorial wrote:@SpringBootApplication is a convenience annotation that adds all of the following:
@Configuration tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath.
This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
Tutorial wrote:At runtime, Spring Data REST will create an implementation of this interface automatically.
Then it will use the @RepositoryRestResource annotation to direct Spring MVC to create RESTful endpoints at /people.
Spring Boot "magically" creates some of the end points that you may need when dealing with data.
However you may still need to add in some extra logic, in which case you would need to use a controller.
This tutorial https://spring.io/guides/gs/rest-service/ titled "Building a RESTful Web Service" does not access any sort of database or use Spring Data.
It simply maps a value form the URL to the output. This is why you need a controller, to handle the mapping and any other logic you may need to program.
Jonathon Roberts wrote:To give more ideas on my API. I basically envisage it needing some authentication (likely OAuth) and authorization. eg authenticated to view x and authorized to edit x.
You may want to look at this tutorial https://spring.io/guides/tutorials/spring-boot-oauth2/ which is a bit more in depth then some of the other ones out there.
I had briefly seen the tutorial you mentioned before, but I'll look into it in more detail.
I'm still trying to fully understand all the annotations and when/where to use them.
But you answered my question! It seems i'll need a controller to to manage the access to certain end points, just wasn't sure if I was missing pieces to Spring and going about it wrong!
Here is a possible idea for this which I was able to do a couple of years ago with ASP.NET using C# and IOS programming.
Jonathon Roberts wrote:I'm looking for my API to then power a website and then iOS / Andoird if necessary.
1) On the server side using Java/Spring create an SQLite database.
2) Have the mobile device download and use that as the database.
3) Have the mobile device update the SQLite database locally.
4) Upload the SQLite database back to the server from the mobile device.
5) Have the server read the SQLite database and process the transactions.
I have not tried this with Android and Java/Spring, however I do have plans in implement this within the next few weeks.