https://github.com/enginooby-practice/spring-rest
Built with Maven, configured by Java code.
https://github.com/enginooby-practice/spring-rest
jackson maven spring-mvc spring-rest
Last synced: about 1 month ago
JSON representation
Built with Maven, configured by Java code.
- Host: GitHub
- URL: https://github.com/enginooby-practice/spring-rest
- Owner: enginooby-practice
- Created: 2020-07-10T07:23:50.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-10T18:04:27.000Z (almost 6 years ago)
- Last Synced: 2025-05-20T03:12:03.471Z (about 1 year ago)
- Topics: jackson, maven, spring-mvc, spring-rest
- Language: Java
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# List of practiced topics
## Configuration
1. Create a Maven web app project
2. Configure POM file
[[pom.xml]()]
- Change JDK version (Servlet 3.0 require JDK 6+)
- ```spring-webmvc``` dependency: support Spring MVC and REST
- ```com.fasterxml.jackson.core/jackson-databind``` dependency: convert JSON
- ```javax.servlet/javax.servlet-api``` dependency: support Servlet
- ```javax.servlet.jsp/javax.servlet.jsp-api``` dependency: support JSP (get rid of Eclipse error)
- ```maven-war-plugin``` plugin: support Maven war plugin
3. Update Maven project (Alt+F5)
4. Configure by Java for Spring MVC
[[Reference]()]
- Spring Config
[[AppConfig]()]
- Spring MVC Initializer
[[SpringMvcInitializer]()]
5. Create a Rest Controller (Service)
[[DemoRestController]()]
- @RestController
- @RequestMapping
- Add endpoint (method)
6. Test Spring REST by running project on server and access the created endpoint (url)
---
## Main Process
1. Create POJO class
[[Student]()]
2. Create Rest Controller with @RestController and @RequestMapping
[[StudentRestController]()]
3. Add endpoints to the Controller
- @PathVariable: bind URL variable to method parameter
---
## Global Exception Handling
1. Create custom Error Response class (POJO)
[[StudentErrorResponse]()]
2. Create custom Exception extends RuntimeException
[[StudentNotFoundException]()]
3. Update REST service to throw exception
[[StudentRestController]()]
4. Create a Exception Handler class with @ControllerAdvice (for Global Handling)
[[StudentRestExceptionHandler]()]
- Add a custom exception handler and a generic exception handler with @ExceptionHandler (for Local Handling, put in the Controller)
---
## Notes/Tips
- If ```src/main/java``` and ```src/test/java``` are not availalbe, go to Build Path -> Order and Export -> Choose JRE and Maven Dependencies.
- Default welcome page: [index.jsp]() inside ```src/main/webapp```
- REST controller automatically converts between JSON and Java POJO using Jackson
- Get the Context Root: ```${pageContext.request.contextPath}```
- Change Servlet 2.3 (generated by archertype webapp) to Servlet 3.0, to use ```${pageContext.request.contextPath}```
- Modify web.xml file in ```src/main/webapp/WEB-INF```
[[web.xml]()]
- Close project and delete it from the workspace (don't delete files on the disk)
- Delete .project and .classpath files and .settings directory from the project folder
- Re-import project: Import -> Existing Maven Project
- Clean the server
- [!] Eclipse default browser does not display JSON -> Use external browsers or Postman
- Append "?" to end of URL to get reload fresh data
- Prefer Global Exception Handling for many controllers using @ControllerAdvice (real-time use of AOP)
- Naming convention for API/endpoints: use only entity, not the action. For example: ```/api/customers/``` instead of ```/api/getCustomers/```
- Path variable: {var} or :var
- Secure Spring REST API
[[URL](https://developer.okta.com/blog/2018/12/18/secure-spring-rest-api)]