REST/Spring/Mokito Flashcards
What is REST
- Representational state transfer
- Architecture style utilising http protocal/methods
- Rest Client + Rest Server
- using URI to identify each resource
- POST, GET, PUT, DELETE
- Resource can be ins format json, XML
What is RESTful web service?
Web service implemented using REST style.
Usually used for:
export data for another application/system
Example: export all USD trades from ourside
What is in HTTP request/response?
request: Http Methods, URI, Http version, Header(key-value pairs), Request Body(msg content)
response: Response code, http version. response header, response body.
Response Code:
200 OK
404 not found
Implementation of Rest end point?
Using Spring framework web
@RestController
@RestMapping(value=”/api/recon”, method = RequestMethod.POST)
public ResponseEntity export(@RequestBody DashboardRequest request);
Using Jersey as a client
Spring:
RestTemplate execute(URL, method, requestCallbacl, requestExtractor)
What is Spring IoC container?
- Core of spring framework, create all the beans,
resolve dependency between them, keep them alive until time to destroy. Managing the whole life cycle of the bean. - Use Dependency Injection
- Most useful one - ApplicationContext
more features than ‘BeanFactory’
can resolve text properties in files
can send event to event listener(ex: when app is started)
Application Main class
@SpringBootApplication
= @EnableAutoConfiguration + @Configuration + @ComponentScan
AutoConfiguration helps what?
for Ex: auto configure JdbcTemplate if its present and a DataSource bean are available
What is SpringBoot?
- A Framework enables fast java app development using spring framework
- Easy to use
- when embedded tomcat server
Difference between Mock and Spy
Mock
create a complete fake object, is the method is not stubbed, it will do nothing, return null
Spy
created a half real object, if the method is not stubbed, it will execute the actual method
Steps in designing REST Services
1.Identify Object Model each object with a unique : int Id; 2. Create Model URIs Follow the resource heriachys /devices /devices/{id}
/configurations
/configurations/{id}
/devices/{id}/configurations
/devices/{id}/configurations/{id}
- Determine Representations - structure, Json, XML
Collections of Resource - only expose necessary fields to reduce API payload
Single resource - can expose mode details fields - Assign HTTP Methods
- More Actions
1) Logging
2) Security
3) Discovery (retry)
https: //restfulapi.net/rest-api-design-tutorial-with-example/#object-model
Constructor Vs setter based DI?
Constructor DI:
enforces the order of initialisation
preventscircular dependencies,
Setter DI:
Setter DI allows circular dependencies
It is easy to miss a few setter calls when wiring the application together.
lazy initialisation - Setter-based DI helps us to inject the dependency only when it is required, as opposed to requiring it at construction time.
log4j
log4j is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4j it is possible to enable logging at runtime without modifying the application binary.