Monday, June 15, 2020

Interview @ Xebia: Round - 2

Hi Friends,

In this post, I am sharing interview questions and answers asked in 2nd round for Java Technical Architect @ Xebia.


Question 1:

What are the different ways of managing REST API versioning?  And how do you manage the versioning for some update() method for the new client?


Answer:

There are multiple ways of managing the REST API versioning:

  • Through a URI path:  We include the version number in the URI path of the endpoint. e.g.:  api/v1/shares
  • Through query parameters: We pass the version number as a query parameter with a specified name e.g.: api/shares?version=1
  • Through custom HTTP headers: We define a new header that contains the version number in the request.
  • Through a content negotiation: The version number is included in the "Accept" header together with the accepted content type.

Suppose we have an update method with endpoint given below. Initially, it was accessible under /v1.0 path. Now, it is available under /v1.1/{id} path.


@PutMapping("/v1.0")
public ShareOld update(@RequestBody ShareOld share){

    return (ShareOld)repository.update(share);

}

@PutMapping("/v1.1/{id}")
public ShareOld update(@PathVariable("id") long id,  @RequestBody ShareOld share){

    return (ShareOld)repository.update(share);

}


And if have a GET mapping that remains same for both versions, then , we can write GET mapping as:

@GetMapping("/v1.0/{id}", "/v1.1/{id}")
public Share findByIdOld(@PathVariable("id") long Id){
    
    return (Share)repository.findById(id);
}


In this, as we have 2 different versions of our API, we need to create two Docket objects using api() method call.


e.g.:

@Bean
public Docket swaggerShareApi10(){

    return new Docket(DocumetationType.SWAGGER_2)
                    .groupName("share-api-1.0")
                    .select()                                .apis(RequestHandlerSelectors.basePackage("pl.piomin.services.versioning.controller"))
.paths(regex("/share/v1.0.*"))
.build()
.apiInfo(new ApiInfoBuilder().version("1.0").title("Share API").description("Documentation Share API v1.0"));

}



@Bean
public Docket swaggerShareApi11(){

    return new Docket(DocumetationType.SWAGGER_2)
                    .groupName("share-api-1.1")
                    .select()                    .apis(RequestHandlerSelectors.basePackage("pl.piomin.services.versioning.controller"))
.paths(regex("/share/v1.1.*"))
.build()
.apiInfo(new ApiInfoBuilder().version("1.1").title("Share API").description("Documentation Share API v1.1"));

}


Now, when we launch Swagger UI, it shows us the dropdown displaying both versions and we can easily switch between them.



Question 2:

What is the difference between MongoDB and Cassandra?

Answer:

  • MongoDB is free and open source , cross platform , document oriented database system. While Cassandra is open source, distributed and decentralized , column-oriented database system.
  • MongoDB does not have triggers while Cassandra has triggers.
  • MongoDB has secondary indexes while Cassandra has restricted secondary indexes.
  • Cassandra uses a selectable replication factor while MongoDB uses a master-slave replication factor.
  • MongoDb is used when we need to store data in JSON style format in some documents which consists of key-value pairs.  While Cassandra is used as decentralized database for big data.




Question 3:

What are the steps for using MongoDB in java project?

Answer:

Steps for using MongoDB are described below:

  • Need to add following dependencies in build.gradle file:
    • compile group: 'org.mongodb', name: 'mongodb-driver', version: '3.11.0'
    • compile group: 'org.mongodb' name: 'bson', version: '3.11.0'
    • compile group: 'org.mongodb' name:'mongodb-driver-core', version: '3.11.0'
    • compile group: 'org.mongodb' name:'mongo-java-driver', version: '3.11.0'
  • Specify the mongo DB URL and mongo DB name in application.yml file.
  • Create MongoClient instance using MongoCredential, MongoClientOptions.Builder and a list of Mongo server addresses.
  • Create MongoCollection bean.
  • Create a class implementing HealthIndicator interface overriding the health() method.
  • Now in the DAO implementation class, call MongoCollection methods such as find(), insertOne(),  countDocuments(), count(), bulkWrite() etc.

That's all for this post.
Thanks for reading!



No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...