InterviewSolution
Saved Bookmarks
| 1. |
Example of @POST rest endpoint? |
|
Answer» Below code structure represent the Rest Controller CLASS developed using SPRING boot framework which use to act as HTTP POST method. import java.util.HashMap; import java.util.Map; // Below series of import is important package specially org.springframework package import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.practise.springboot.Product; // Need to mention the RestController so that it will behave as rest end point. @RestController public class ProductServiceController { private static Map<String, Product> productRepo = new HashMap<>(); // Below method works as a POST method, which is responsible to receive the HTTP Post //method call along with RequestBody which has a product information, need to persist in //Database and will return the HTTP Status as CREATED. @RequestMapping(value = "/products", method = RequestMethod.POST) public ResponseEntity<Object> createProduct(@RequestBody Product product) { productRepo.post(product.getId(), product); return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED); } } } |
|