InterviewSolution
Saved Bookmarks
| 1. |
Example of @PUT rest endpoint? |
|
Answer» Below code structure represent the Rest Controller class developed USING spring boot framework which USE to act as Http Put 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.PathVariable; 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 responsible to handle the HTTP PUT request, which will receive the Path //variable {id } as a parameter and responsible to update the database information according // to the id parameter. @RequestMapping(value = "/products/{id}", method = RequestMethod.PUT) public ResponseEntity<Object> updateProduct(@PathVariable("id") String id, @RequestBody Product product) { productRepo.remove(id); product.setId(id); productRepo.put(id, product); return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK); } } |
|