This simple Calculator demonstrates the usage of Spring Boot Rest service and Rest Client
Service Steps involved:
1) Maven Dependencies
2) Implement model class
3) Implement service class
4) Injecting resources into service class
5) Service class Configuration
6) Running Service and port settings
Client Steps involved:
1) Maven dependencies
2) RestTemplate instantiation
3) Creating Headers
4) Creating Request Parameters
5) Dealing with requests and responses
Service Steps involved:
1) Maven Dependencies and package structure
Spring Boot dependency:
<dependency>
Spring Rest dependency:
<dependency>
2) Implement model class
3) Implement service class
4) Injecting resources into service class
5) Service class Configuration
Service Steps involved:
1) Maven Dependencies
2) Implement model class
3) Implement service class
4) Injecting resources into service class
5) Service class Configuration
6) Running Service and port settings
Client Steps involved:
1) Maven dependencies
2) RestTemplate instantiation
3) Creating Headers
4) Creating Request Parameters
5) Dealing with requests and responses
Service Steps involved:
1) Maven Dependencies and package structure
Spring Boot dependency:
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Spring Rest dependency:
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>
Package Structure:2) Implement model class
package com.calculator.app.calculatorapp.model;
import org.springframework.stereotype.Component; import java.math.BigDecimal; @Componentpublic class CalculatorModel { public BigDecimal add(BigDecimal number1 , BigDecimal number2){ return new BigDecimal(number1.intValue() +
number2.intValue()); } }
3) Implement service class
4) Injecting resources into service class
5) Service class Configuration
import com.calculator.app.calculatorapp.model.CalculatorModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
@RestController public class CalculatorService { @Autowired private CalculatorModel calculatorModel; @GetMapping(value = "/add") public BigDecimal addNumbers(@RequestParam(name = "number1")
BigDecimal number1, @RequestParam(name = "number2")
BigDecimal number2){ return calculatorModel.add(number1,number2); } }6) Running Service and Port settings
application.properties
server.port=7070
package com.calculator.app.calculatorapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class CalculatorAppApplication { public static void main(String[] args) { SpringApplication.run(CalculatorAppApplication.class, args); } }
The service starts on Port number 7070 and can be accessed via Http url
http://localhost:7070/add?number1=34&number2=44
Client Steps involved:1) Maven dependencies<dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>2) RestTemplate instantiation3) Creating Headers4) Creating Request Parameters5) Dealing with requests and responsespublic static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); UriComponentsBuilder uriComponentsBuilder =UriComponentsBuilder.fromHttpUrl("http://localhost:7070/add").queryParam("number1",344334). queryParam("number2",434343433); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<BigDecimal> sum = restTemplate.exchange(uriComponentsBuilder.toUriString(),HttpMethod.GET,entity,BigDecimal.class); System.out.println(sum.getBody()); }
No comments:
Post a Comment