Class ProductController

java.lang.Object
com.example.springrest.controllers.ProductController

@RestController @RequestMapping(value="/api/products", produces="application/json") public class ProductController extends Object
REST controller for managing products.

Responsibilities:

  • List and search products with pagination & sorting.
  • Create, update, and delete products.
  • Return 404 for missing resources (via the service’s getOrThrow).

Notes:

  • Parameter-level validation annotations (e.g., @Min(1) on id) are included for clarity, but they will not be enforced until we introduce @Validated in Part III of the book.
  • For now, invalid IDs (e.g., 0 or negative numbers) will flow through to the service layer and typically result in a 404 Not Found via ProductService.getOrThrow(Long).
  • This keeps the early chapters simpler, while still hinting at stronger validation to come.

Examples:


 # List (page 0, size 20, sort by id DESC)
 curl 'http://localhost:8080/api/products?page=0&size=20&sort=id,desc'

 # Search by name (case-insensitive)
 curl 'http://localhost:8080/api/products/search?q=mug&page=0&size=10'

 # Read one
 curl 'http://localhost:8080/api/products/42'

 # Create
 curl -X POST 'http://localhost:8080/api/products' \
      -H 'Content-Type: application/json' \
      -d '{"name":"Coffee Mug","price":12.99}'

 # Update (PUT is idempotent here)
 curl -X PUT 'http://localhost:8080/api/products/42' \
      -H 'Content-Type: application/json' \
      -d '{"name":"Travel Mug","price":14.50}'

 # Delete
 curl -X DELETE 'http://localhost:8080/api/products/42'
 
Since:
1.0
  • Constructor Details

    • ProductController

      public ProductController()
  • Method Details

    • getAll

      @GetMapping public org.springframework.data.domain.Page<ProductResponse> getAll(@PageableDefault(size=20,sort="id",direction=DESC) org.springframework.data.domain.Pageable pageable)
      Returns a paginated list of products.

      Default page size is 20, sorted by id DESC. Override with query params like ?page=1&size=50&sort=name,asc.

      Parameters:
      pageable - pagination invalid input: '&' sorting (page, size, sort)
      Returns:
      page of ProductResponse
    • search

      @GetMapping("/search") public org.springframework.data.domain.Page<ProductResponse> search(@RequestParam("q") String q, @PageableDefault(size=20,sort="id",direction=DESC) org.springframework.data.domain.Pageable pageable)
      Searches products by (case-insensitive) name substring.
      Parameters:
      q - required query string to match within product names
      pageable - pagination invalid input: '&' sorting (page, size, sort)
      Returns:
      page of matches
    • getById

      @GetMapping("/{id}") public org.springframework.http.ResponseEntity<ProductResponse> getById(@PathVariable @Min(value=1L,message="ID must be >= 1") @Min(value=1L,message="ID must be >= 1") Long id)
      Reads a product by id.
      Parameters:
      id - product id (≥ 1)
      Returns:
      the product
    • create

      @PostMapping(consumes="application/json") public org.springframework.http.ResponseEntity<ProductResponse> create(@RequestBody @Valid @Valid ProductRequest request)
      Creates a new product.

      On success returns 201 Created with a Location header pointing to /api/products/{id}.

      Parameters:
      request - validated product payload
      Returns:
      created product (body) and Location header
    • update

      @PutMapping(value="/{id}", consumes="application/json") public org.springframework.http.ResponseEntity<ProductResponse> update(@PathVariable @Min(value=1L,message="ID must be >= 1") @Min(value=1L,message="ID must be >= 1") Long id, @RequestBody @Valid @Valid ProductRequest request)
      Replaces a product’s name and price.

      Semantically a full update (PUT). If you later add PATCH, you can reuse the mapper’s partial update semantics.

      Parameters:
      id - product id (≥ 1)
      request - validated payload
      Returns:
      updated product
    • delete

      @DeleteMapping("/{id}") public org.springframework.http.ResponseEntity<Void> delete(@PathVariable @Min(value=1L,message="ID must be >= 1") @Min(value=1L,message="ID must be >= 1") Long id)
      Deletes a product by id.
      Parameters:
      id - product id (≥ 1)
      Returns:
      204 No Content on success