• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Should I create a Repository interface and Models based on the end points in a REST app?

 
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Spring Boot and rusty in REST...

Now, I am tasked to created several end points based on the following tags:



My question is :

Should I create a Store repository interface or Do I create an Order repository interface and then I do it like this ;



Or should I create a Order repository interface  and then I do it like this



Assuming the later is to be used, I would need to have a entity class Store right ?

Thanks.
 
Saloon Keeper
Posts: 12428
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Design repositories and web APIs separately. While REST resources may often look like database entities, there are cases where they don't map to each other well.

A StoreRepository only makes sense if you save individual pet stores in a database.

Your REST API doesn't make sense to me. A POST request on a path /store/inventory implies to me that you will be adding an entire inventory to a store, not placing an order for a pet.

It also doesn't make sense to me that you would use a Pet instance as an ID to get an Order from your repository. Instead, I would expect the OrderRepository to look something like this:
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Design repositories and web APIs separately. While REST resources may often look like database entities, there are cases where they don't map to each other well.

A StoreRepository only makes sense if you save individual pet stores in a database.

Your REST API doesn't make sense to me. A POST request on a path /store/inventory implies to me that you will be adding an entire inventory to a store, not placing an order for a pet.



I think I better provide the various end points to be more clear:

But, really, I am blur myself...



I have left out all the common findbyId in /pet/ end point.

Hope you could advise me if there is any change in terms of the Repository interface I should create.

Many thanks for your help again.

 
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find this tutorial helpful. You can create a repository class extending the CrudRepository .
https://www.concretepage.com/spring-5/spring-data-crudrepository-example
 
Stephan van Hulst
Saloon Keeper
Posts: 12428
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The endpoints you described do not form a REST API. It's just plain RPC. I'm not saying that's bad, it's fine. It's just not REST.

Based purely on the request URL paths, you'll want at least a PetController and an OrderController, and maybe an InventoryController. For instance, the PetController could look like this (when using JAX-RS):

Note that I made up the classes FormDataUtilities and InvalidFormDataImageException, they are not part of any framework I know of.

Anyway, as you can see from this example controller, it assumes that there is a PetRepository interface that contains methods findByStatuses() and findById() that return Collection<Pet> and Optional<Pet> respectively, and a method addImage().

Only make repository interfaces for entire object graphs that you want to store. For instance, even if you have a separate table in your database that stores images, don't create an ImageRepository because you'll only ever want to store or retrieve an image related to another specific object in your database, such as a pet. You'll probably want a PetRepository and an OrderRepository, and you'll probably want an InventoryRepository as well that stores the inventory of a store.

I'm not sure what your supervisor expects of the GET /store/inventory endpoint, but I imagine it would return a response like this:

Such a controller could look like this:
 
Himai Minh
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the above example, in Spring, we use @Autowired to inject the PetRepository.
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The endpoints you described do not form a REST API. It's just plain RPC. I'm not saying that's bad, it's fine. It's just not REST.

Based purely on the request URL paths, you'll want at least a PetController and an OrderController, and maybe an InventoryController. For instance, the PetController could look like this (when using JAX-RS):



Oh no....cos I actually tweaked a sample Spring REST project to fit into this requirements.

The sample project I used is :

https://projects.spring.io/spring-petclinic/#quick-start

And the this project will be using JPA so I have used extened JpaCRUDRepository in all my interfaces.  Is it ok to do it that way ?

Furthermore, I am thinking of later abstracting out the common findById across all the entities by using


SpEL Expressions as outlined in 5.3.7 at https://docs.spring.io/spring-data/jpa/docs/current/reference/html/@reference

Cos there are so many CRUD and I am thinking just replacing it with a T<CRUD>(Note: I am pretty new in this but I have seen people doing it this way)



But, I am still struggling with the basic skeleton to get even the base up and running cos I find that there seems to be a tight coupling with the sample that downloaded from Spring site as in there is a validation maven plug in and if you added new entities then it is not going to run.  I spent yesterday night fixing that.

I hope you can let me have some clue if I am on the right direction in doing this project. Tks.

Also, later on I will be adding User log in function with session and really I am not sure how this piece fit together...it seems more like a MVC app to me...correct me if I am wrong...


/user/createWithList:
   post:
     tags:
     - "user"
     summary: "Creates list of users with given input array"
     description: ""
     operationId: "createUsersWithListInput"
     produces:
     - "application/xml"
     - "application/json"

/user/login:
   get:
     tags:
     - "user"
     summary: "Logs user into the system"
      operationId: "loginUser"
 
     parameters:
     - name: "username"
       in: "query"
       description: "The user name for login"
       required: true
       type: "string"
     - name: "password"
       in: "query"
       description: "The password for login in clear text"
       required: true
       type: "string"



And since it is written creates list of users with given input array I supposed this is a hardcoded function with List<User> add UserName blah blah blah

Hope to have your guidance Stephan and many thanks for your help.

 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:You may find this tutorial helpful. You can create a repository class extending the CrudRepository .
https://www.concretepage.com/spring-5/spring-data-crudrepository-example



Hi Himai,

Just to clarify, using this would be not different from using Spring Boot right ?
Or I still have to use Spring Initizlizer to generate all the libraries that are needed before I try to use it in my case?
 
Himai Minh
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Targara,
The CrudRepository can be used in Spring boot / Spring framework.
You can use Spring intializer to generate a Spring boot project and add some child classes for CrudRepository.
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, Targara,
The CrudRepository can be used in Spring boot / Spring framework.
You can use Spring intializer to generate a Spring boot project and add some child classes for CrudRepository.



Hi Himai Minh,

I would like to further check with you cos I found out that there is this petstore that has very similar end points like what I am doing now but they are using ResponseContext to return the response.


https://github.com/swagger-api/swagger-petstore/blob/master/src/main/java/io/swagger/petstore/controller/PetController.java



Is this the right way to do a RPC web app?

Can I use javax.ws.rs-api to do it ?

And if I don't do the testing like what they do using Petdata, do I just use a public void main (String []) and then I just create an instance of Pet and do a getId, getName like that is it ok ?

Furthermore, I'd like to clarify should I use the below code for /store/order


cos I am not sure about this one...since I can't tell if a user is supposed to be able to place an order just by looking at this description : /store/order and should I included Store as entity ?

Really, I am so confused really hope can get some help here....really I feel that maybe coding is not for me...

Tks.

 
Himai Minh
Bartender
Posts: 1955
13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Tanagara,
The PetController is not using any Spring's Restful features.
I am sure javax.ws.rs-api is from JEE,not Spring
In Spring , you can use ResponseEntity<>  to return the output.

You may want to use these as reference:
https://spring.io/guides/tutorials/rest/
https://www.baeldung.com/spring-response-entity
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi,Tanagara,
The PetController is not using any Spring's Restful features.
I am sure javax.ws.rs-api is from JEE,not Spring
In Spring , you can use ResponseEntity<>  to return the output.

You may want to use these as reference:
https://spring.io/guides/tutorials/rest/
https://www.baeldung.com/spring-response-entity



Thanks so much for the tutorial and I have used this guide and adapt accordingly.

But, another question came to my mind as I edit the PetController... cos for if I were to follow strictly the example in that swagger-petstore, I would just be getting a return in response as oppose to the traditional way of returning via GET, POST etc to return say an array of pets that has the same status for example.

I'd also want to clarify about java.ws.rs, cos in the utility of this petstore, they are also using import javax.ws.rs.core.MediaType so is it not advisable to use or it is ok to use?

Or ...I am sorry but I have problem in reading that swagger document...does it mean that I only need to return a response will do.  But then if it is just a response of ok or bad request, then what does this app serve the purpose then...so sorry but I really am not very good in this whole thing.

Thanks for your help in advance.
 
Himai Minh
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Tangara,
For MediaType, you can use org.spring.framework.http.MediaType. There are different types in this class, such as APPLICATION_JSON, APPLICATION_ATOM_XML and etc.
If you choose to use Spring framework, then stick with it and don't need to use JEE for Restful.

If can return ResponseEntity with a status such as new ResponseEntity<>( message, HttpStatus.OK) or new ResposneEntity<>(message, HttpStatus.BAD_REQUEST) and etc.
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, Tangara,
For MediaType, you can use org.spring.framework.http.MediaType. There are different types in this class, such as APPLICATION_JSON, APPLICATION_ATOM_XML and etc.
If you choose to use Spring framework, then stick with it and don't need to use JEE for Restful.

If can return ResponseEntity with a status such as new ResponseEntity<>( message, HttpStatus.OK) or new ResposneEntity<>(message, HttpStatus.BAD_REQUEST) and etc.



Hi Himai,

That part is easy but the utility part if I were to use Spring HTTP headers I am afraid with my beginner level it is really tough to refactor.

I google abit and found this one at

https://stackoverflow.com/questions/62372402/spring-how-to-pass-automatically-https-header-between-microservices

Do you think this is the thing I should use or can you give me some hints how to refactor the utility part to accept json and xml using header request in Spring.
Tks.
 
Himai Minh
Bartender
Posts: 1955
13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Spring, your request is converted into Json by default.
You pass in a customized object type to a method, it will be converted into JSON by Spring.
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:In Spring, your request is converted into Json by default.
You pass in a customized object type to a method, it will be converted into JSON by Spring.



Thanks Himai,

Could you advise me at what stage people start to test out or do they test out Rest Controller at all ?  Or do I just use the RestClient as per your recommend article to do it.

I have often heard people using Postman or Curl or even Swagger to test out the end point so I just hope to have some advice where to start ?

Furthermore, I have tried using that sample app and modified accordingly to use Spring library as suggested by you.

However, there is this pets.add(createPet) that I can't get into the RespondEntity and Request Context, I hope you can let me know if there is a better way to do it other than using the PetData.

Thanks again for your help.
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, Tangara,
For MediaType, you can use org.spring.framework.http.MediaType. There are different types in this class, such as APPLICATION_JSON, APPLICATION_ATOM_XML and etc.
If you choose to use Spring framework, then stick with it and don't need to use JEE for Restful.

If can return ResponseEntity with a status such as new ResponseEntity<>( message, HttpStatus.OK) or new ResposneEntity<>(message, HttpStatus.BAD_REQUEST) and etc.



Hi Himai,

I need further clarification as  I noted that I am mixing RequestContext request in this ResponseEntity and I am not sure if it is alright to do that.  I just want to make sure that everything is correct, since you said don't sue JAX-RS.

Here'a a snippet of code


You advice is greatly appreciate.

 
Himai Minh
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try and see if that works.
 
Himai Minh
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, one more note. I think you only need to pass the integer id to the delete method. You don't need the RequestContext.
 
tangara goh
Ranch Hand
Posts: 648
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:

Himai Minh wrote:You may find this tutorial helpful. You can create a repository class extending the CrudRepository .
https://www.concretepage.com/spring-5/spring-data-crudrepository-example




Hi Himai,

I refer to the above recommended sample.

Now, I am facing problem in using Postman to test my SpringBoot, as it keeps giving me

CORS error : the request has been blocked because of CORS policy


and I have been trying for days how to resolve this without avail...I used annotations like @CrossOrigin at the Controller and a the method, removing all the security config files etc, etc..still cannot

So, now I tried out using RESTclient like what is is mentioned in this article.

But, I am not getting any result either.

How do I run this RESTClient ?

Futhermore, I am still trying to get SpringBoot to connect to MYSQL(fumbled till now...)

so I test out using hard-coded Status :



Hope you could share with me

1) what should I do to get rid of the CORS error by Postman
2) how to run the SpringBoot with the RestClientUitl to see the result ?

Thank you once again for your help.

 
Stephan van Hulst
Saloon Keeper
Posts: 12428
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand what CORS is in the first place? Do you understand what causes CORS issues?

What request are you making in Postman? Can you show us the controller action that you expect to be called?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic