Rest Backend with Java Spring , JDBCTemplate and Oracle 11g
In this post i’ll show how to build simple rest backend using by java spring and jdbctemplate
In this project we have an ER Diagram for specific requirements.
I created some Models in java for our objects and mappings.
Created 2 controllers for two different situtations. My case these was Admins and users.
@PreAuthorize("hasAuthority('Admin')") @PostMapping("/author/add") public ResponseEntity<Author>addAuthor(@RequestBody Author author){ return new ResponseEntity<>(authorService.addAuthor(author),HttpStatus.OK); }
Using preauthorize annotations for giving access to admins. Others cannot access these links.
After that i have to define services and repositories.
Used interfaces to understand and show better view of editing and reviewing.
Repos looks like these:
Sample code for AuthorRepository
public List<Author> getAllAuthors() { List<Author> tmp = jdbcTemplate.query("select authorID,authorName,authorLastName from FAHRI2.AUTHOR ORDER BY authorID" ,(rs,rowNum) ->new Author(rs.getInt("authorID"),rs.getString("authorName"),rs.getString("authorLastName"))); return tmp; }
Used Oracle DB 11g for this project and Spring JDBCTemplate for db connections.
Codes in my github repo check out all codes here. https://github.com/ffahri/LibraryAutomationBackend
When we login the system using Postman
localhost:8090/oauth/token
We should use basic auth for getting oAuth token.
{ "access_token": "your_access_token_will_be_here", "token_type": "bearer", "expires_in": 36000, "scope": "read write", "jti": "your-jti-will-be-here" }
Using this token to list all authors.
Specify Get method because we used @GetMapping in our code. And we show our authorlist