package com.controllers;
@RestController
public class ProductController {
private ProductService productService;
public static final String PLAIN_RESPONSE_TYPE = "text/plain";
public static final String JSON_RESPONSE_TYPE = "application/json";
public static final String COLLECTION_NAME = "productCollection";
boolean USE_CF = true;
@Autowired
public void setProductService() {
this.productService = new ProductServiceImpl(COLLECTION_NAME, USE_CF, MONGO_LOCAL_HOST_URL);
}
@ApiOperation(value = "Creates a product")
@RequestMapping(value = "/product/create", method = RequestMethod.POST, produces = PLAIN_RESPONSE_TYPE)
public ResponseEntity<?> addProduct(@RequestBody Product product) {
Product createdProduct = productService.createProduct(product);
return new ResponseEntity<>("New Product added successfully", HttpStatus.OK);
}
@ApiOperation(value = "Updates a product")
@RequestMapping(value = "/product/update/{productId}", method = RequestMethod.PUT, produces = PLAIN_RESPONSE_TYPE)
public ResponseEntity<?> updateProduct(@PathVariable String productId, @RequestBody Product updatedProduct) {
productService.updateProduct(updatedProduct);
return new ResponseEntity<>("Product updated successfully", HttpStatus.OK);
}
@ApiOperation(value = "List all available products", response = Iterable.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfull in finding the list"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
@RequestMapping(value = "/product/list", method = RequestMethod.GET, produces = "application/json")
public Iterable<Product> list() {
Iterable<Product> productList = productService.findAllProducts();
return productList;
}
@ApiOperation(value = "Find product with an Id", response = Product.class)
@RequestMapping(value = "/product/find/{productId}", method = RequestMethod.GET, produces = JSON_RESPONSE_TYPE)
public Product findProduct(@PathVariable String productId) {
Product product = productService.getProductById(productId);
return product;
}
@ApiOperation(value = "Delete a product")
@RequestMapping(value = "/product/delete/{productId}", method = RequestMethod.DELETE, produces = PLAIN_RESPONSE_TYPE)
public ResponseEntity<?> delete(@PathVariable String productId) {
productService.deleteProduct(productId);
return new ResponseEntity<>("Product deleted successfully", HttpStatus.OK);
}
}
package com.controllers;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
.apiInfo(apiInfo()).select().paths(postPaths()).build();
}
private Predicate<String> postPaths() {
return PathSelectors.regex("/product.*");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Product Catalog listing")
.description("Product Catalog reference for developers")
.version("1.0").build();
}
}
{"swagger":"2.0","info":{"description":"Product Catalog reference for developers","version":"1.0","title":"Product Catalog listing"},"host":"springbootswagger-nonbureaucratic-baseline.cfapps.eu10.hana.ondemand.com","basePath":"/","tags":[{"name":"product-controller","description":"Product Controller"}],"paths":{"/product/create":{"post":{"tags":["product-controller"],"summary":"Creates a product","operationId":"addProductUsingPOST","consumes":["application/json"],"produces":["text/plain"],"parameters":[{"in":"body","name":"product","description":"product","required":true,"schema":{"$ref":"#/definitions/Product"}}],"responses":{"200":{"description":"OK","schema":{"type":"object"}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}},"/product/delete/{productId}":{"delete":{"tags":["product-controller"],"summary":"Delete a product","operationId":"deleteUsingDELETE","consumes":["application/json"],"produces":["text/plain"],"parameters":[{"name":"productId","in":"path","description":"productId","required":true,"type":"string"}],"responses":{"200":{"description":"OK","schema":{"type":"object"}},"401":{"description":"Unauthorized"},"204":{"description":"No Content"},"403":{"description":"Forbidden"}}}},"/product/find/{productId}":{"get":{"tags":["product-controller"],"summary":"Find product with an Id","operationId":"findProductUsingGET","consumes":["application/json"],"produces":["application/json"],"parameters":[{"name":"productId","in":"path","description":"productId","required":true,"type":"string"}],"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/Product"}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}},"/product/list":{"get":{"tags":["product-controller"],"summary":"List all available products","operationId":"listUsingGET","consumes":["application/json"],"produces":["application/json"],"responses":{"200":{"description":"Successfull in finding the list","schema":{"$ref":"#/definitions/Iterable"}},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"The resource you were trying to reach is not found"}}}},"/product/update/{productId}":{"put":{"tags":["product-controller"],"summary":"Updates a product","operationId":"updateProductUsingPUT","consumes":["application/json"],"produces":["text/plain"],"parameters":[{"name":"productId","in":"path","description":"productId","required":true,"type":"string"},{"in":"body","name":"updatedProduct","description":"updatedProduct","required":true,"schema":{"$ref":"#/definitions/Product"}}],"responses":{"200":{"description":"OK","schema":{"type":"object"}},"201":{"description":"Created"},"401":{"description":"Unauthorized"},"403":{"description":"Forbidden"},"404":{"description":"Not Found"}}}}},"definitions":{"Iterable«Product»":{"type":"object"},"Iterable":{"type":"object"},"Product":{"type":"object","properties":{"description":{"type":"string"},"imageUrl":{"type":"string"},"price":{"type":"number","format":"double"},"productId":{"type":"string"}}}}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
40 | |
25 | |
17 | |
13 | |
8 | |
7 | |
7 | |
7 | |
6 | |
6 |