I am very new to Spring Boot and trying out different things.
I have a class in which a method does simple calculation, accepts two numbers and give addition.Now i want to pass the numbers through api in json format and return the addition of the number.
Can we pass the variables in a @POSTMapping
and return the result ?
Controller class
@RestController
@RequestMapping(value="/TC")
public class CountSpringAppController {
@Autowired
private CountService countService;
@PostMapping(value="/add/{number1}/{number2}")
public int getCount(@PathVariable int num1,@PathVariable int num2) {
return countService.count(num1, num2);
}`
service class
@Service
public class CountService {
public int count(int num1, int num2) {
return num1+num2;
}
}
input
{
"num1":1,
"num2":1
}
output
2