Step 1) Create new spring starter project.
Step 2) Mention below configurations. Click Next.
Click next and select checkboxes below. This will bring jars automatically to our project.
Click Finish.
Below will be project structure you will see in explorer.
Now try running JbkSpringBootRestApiApplication.java (you can change this to any name suitable for our project).
If you see below logo on console means our application started correctly.
Now go ahead and create REST API.
Write pojo which holds data in format of java object.
Transaction.java as a standard add constructor getter and setters.
package com.javabykiran;
@Component
public class Transaction {
String transId;
String transactionDoneBy;
String trasSource;
public Transaction(String transId, String transactionDoneBy, String trasSource) {
super();
this.transId = transId;
this.transactionDoneBy = transactionDoneBy;
this.trasSource = trasSource;
}
public Transaction() {
super();
}
public String getTransId() {
return transId;
}
public void setTransId(String transId) {
this.transId = transId;
}
public String getTransactionDoneBy() {
return transactionDoneBy;
}
public void setTransactionDoneBy(String transactionDoneBy) {
this.transactionDoneBy = transactionDoneBy;
}
public String getTrasSource() {
return trasSource;
}
public void setTrasSource(String trasSource) {
this.trasSource = trasSource;
}
}
Now I will create endpoint in controller. This is a class where user requests will be coming in. This class will be able to produce out put in json or xml format.
The entity which sends request has to consume this response and may be rendered on UI.
TrasactionController.java
package com.javabykiran;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TransactionController {
@Autowired
Transaction transaction;
@RequestMapping("/transaction/get")
public Transaction getTransaction() {
return transaction;
}
}
If you observe we have Transaction and Transaction controller in other package so we need to tell spring boot class where my controllers and pojo are.
One more thing whenever we write Transaction.java to be autowired annotate this class with @Service or @Repository or @Component all are same but for used different purposes as per design.
I will use @component as this is just pojo. But if some class is doing persistence logic or database operation I may go for @Repository.
If lot of business logic is handled I will go with @Service. These are common conventions people are using.
Main class will look like this.
package com.javabykiran.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.javabykiran")
public class JbkSpringBootRestApiExApplication {
public static void main(String[] args) {
SpringApplication.run(JbkSpringBootRestApiExApplication.class, args);
}
}
Run application you will see below on browser.
One trouble shooting I will do here if below is error.
The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.
Then stop server first by clicking below red button in console.
After this you will see terminate button click that.
Click that and run main class again.
This appeared means we are good to go.
Go to browser put URL as http://localhost:8080/transaction/get in address bar.
Now let’s put logic in controller to see proper values in response.
Modify method as below:
@RequestMapping("/transaction/get")
public Transaction getTransaction() {
transaction.setTransId("1234");
transaction.setTrasSource("Amazon");
transaction.setTransactionDoneBy("JBK");
return transaction;
}
In response on browser you will see:
{"transId":"1234","transactionDoneBy":"JBK","trasSource":"Amazon"}
Now we will see some more things
@RequestMapping("/transaction/getId")
public Transaction getTransaction(
@RequestParam(name = "transId", required = false, defaultValue = "Unknown") String transId) {
transaction.setTransId(transId);
transaction.setTrasSource("Amazon");
transaction.setTransactionDoneBy("JBK");
return transaction;
}
URL: http://localhost:8080/transaction/getId
Response will be see if nothing provided in request default value will be taken.
{"transId":"Unknown","transactionDoneBy":"JBK","trasSource":"Amazon"}
URL: http://localhost:8080/transaction/getId?transId=890
Response will be as below:
{"transId":"890","transactionDoneBy":"JBK","trasSource":"Amazon"}
In controller add method as below:
@RequestMapping("/transaction/getAllTrans")
public List<Transaction> getAllTransaction() {
List<Transaction> listTransaction=new ArrayList<>();
Transaction trans=new Transaction("123","kiran","Flipkart");
Transaction trans1=new Transaction("456","jbk","Irctc");
listTransaction.add(trans);
listTransaction.add(trans1);
return listTransaction;
}
URL: http://localhost:8080/transaction/getAllTrans
[{"transId":"123","transactionDoneBy":"kiran","trasSource":"Flipkart"},
{"transId":"456","transactionDoneBy":"jbk","trasSource":"Irctc"}]