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.
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):
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)
/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"
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
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.
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
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.
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.
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.
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
CORS error : the request has been blocked because of CORS policy
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
Himai Minh wrote:Hi Tangara,
You mean the difference between @Controller and @RestController?
@Controller is the C of the MVC architecture , which handles the update of model,M and dispatch the model to view V.
@RestController is also the controller , but with this annotation , all the methods don't need @ResponseBody.
https://www.baeldung.com/spring-controller-vs-restcontroller
Usually in a controller, it injects a service bean and the service bean interacts with the repository bean to access the database.
For the difference between @ResponseEntity and @ResponseBody:
http://zetcode.com/springboot/responseentity/#:~:text=ResponseEntity%20represents%20an%20HTTP%20response,add%20headers%20and%20status%20code.
https://goodwinwei.wordpress.com/2017/01/06/springresquestbody-responsebody-vs-httpentityresponseentity/
Repository is a raw type. References to generic type Repsository<T1, T2> should be parameterized
http://zetcode.com/springboot/repository/
Himai Minh wrote:Hi Tangara,
You mean the difference between @Controller and @RestController?
@Controller is the C of the MVC architecture , which handles the update of model,M and dispatch the model to view V.
@RestController is also the controller , but with this annotation , all the methods don't need @ResponseBody.
https://www.baeldung.com/spring-controller-vs-restcontroller
Usually in a controller, it injects a service bean and the service bean interacts with the repository bean to access the database.
For the difference between @ResponseEntity and @ResponseBody:
http://zetcode.com/springboot/responseentity/#:~:text=ResponseEntity%20represents%20an%20HTTP%20response,add%20headers%20and%20status%20code.
https://goodwinwei.wordpress.com/2017/01/06/springresquestbody-responsebody-vs-httpentityresponseentity/
Himai Minh wrote:Also, one more note. I think you only need to pass the integer id to the delete method. You don't need the RequestContext.
@ComponentScan("com.petstore.repository.PetRepository")
//@EnableJpaRepositories(basePackageClasses = PetRepository.class)
//@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class MyPetStoreApplication {
private static final Logger LOG = LoggerFactory.getLogger(MyPetStoreApplication.class);
//@Autowired
//DataSourceProperties dataSource;
//@Autowired
//PetRepository petRepository;
public static void main(String[] args) { //
PetRepository petRepository = SpringApplication.run(MyPetStoreApplication.class, args).getBean(PetRepository.class);
petRepository.save(new Pet(1, "aaa"));
petRepository.save(new Pet(2, "bbb"));
}
//public Pet(Integer petId, String name) {// I created another constructor just with 2 fields
}
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.petstore.repository.PetRepository' available.
Consider Paul's rocket mass heater. |