Skip to content

Commit

Permalink
Created catalog microservice demo based on Spring Boot
Browse files Browse the repository at this point in the history
  • Loading branch information
azzazzel committed Mar 15, 2015
1 parent cdebcbb commit 0f8a0ce
Show file tree
Hide file tree
Showing 14 changed files with 526 additions and 1 deletion.
1 change: 1 addition & 0 deletions catalog-microservice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
69 changes: 69 additions & 0 deletions catalog-microservice/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>dukes-forest</artifactId>
<version>7.0.6-SNAPSHOT</version>
</parent>
<artifactId>catalog-microservice</artifactId>
<name>Catalog microservice</name>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>usecases-catalog</artifactId>
<version>7.0.6-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.jsondoc</groupId>
<artifactId>spring-boot-starter-jsondoc</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.jsondoc</groupId>
<artifactId>jsondoc-ui-webjar</artifactId>
<version>1.1.12</version>
</dependency>

</dependencies>

<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.forest.micro.catalog;

import org.jsondoc.spring.boot.starter.EnableJSONDoc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan
@EnableJSONDoc
public class CatalogApp {

public static void main(String[] args) throws Exception {
SpringApplication.run(CatalogApp.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.forest.micro.catalog;

import java.util.List;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiAuthNone;
import org.jsondoc.core.annotation.ApiBodyObject;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiPathParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.jsondoc.core.annotation.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
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.forest.micro.catalog.model.CategoryObject;
import com.forest.micro.catalog.wiring.CategoryManager;
import com.forest.model.Category;

@RestController
@Api(name = "Category", description = "Methods for managing categories")
@ApiVersion(since = "1.0")
@ApiAuthNone
public class CategoryEndpoint {

private CategoryManager categoryManager;

@Autowired
public CategoryEndpoint(CategoryManager categoryManager) {
this.categoryManager = categoryManager;
}

@ApiMethod(produces = "application/json", description = "Get a list of all categories")
@ApiResponseObject
@RequestMapping("/cetegories")
List<Category> getCategories() {
return categoryManager.getAll();
}

@ApiMethod(produces = "application/json", description = "Returns a caregory with given id")
@ApiResponseObject
@RequestMapping(value = "/category/{id}", produces = "application/json")
CategoryObject getCategory(
@PathVariable("id") @ApiPathParam(description = "The id of the category") int id) {
return (CategoryObject) categoryManager.getCategory(id);
}

@ApiMethod(produces = "application/json", description = "Creates new category")
@ApiResponseObject
@RequestMapping(method = RequestMethod.POST, value = "/category", consumes = "application/json")
void addCategory(@RequestBody @ApiBodyObject CategoryObject category) {
categoryManager.createCategory(category);
}

@ApiMethod(produces = "application/json", description = "Updates existing category")
@ApiResponseObject
@RequestMapping(method = RequestMethod.PUT, value = "/category/{id}", consumes = "application/json")
void updateCategory(
@PathVariable("id") @ApiPathParam(description = "The id of the category") int id,
@RequestBody @ApiBodyObject CategoryObject category) {

category.setId(id);
categoryManager.updateCategory(category);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.forest.micro.catalog;

import java.util.List;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiAuthNone;
import org.jsondoc.core.annotation.ApiBodyObject;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiPathParam;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.jsondoc.core.annotation.ApiVersion;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.forest.micro.catalog.model.ProductObject;
import com.forest.micro.catalog.wiring.ProductManager;
import com.forest.model.Product;

@RestController
@Api(name = "Product", description = "Methods for managing products")
@ApiVersion(since = "1.0")
@ApiAuthNone
public class ProductEndpoint {

private ProductManager productManager;

@Autowired
public ProductEndpoint(ProductManager productManager) {
this.productManager = productManager;
}

@ApiMethod(produces = "application/json", description = "Get a list of all products")
@ApiResponseObject
@RequestMapping("/products")
List<Product> getProducts() {
return productManager.getAll();
}

@ApiMethod(produces = "application/json", description = "Returns a product with given id")
@ApiResponseObject
@RequestMapping(value = "/product/{id}", produces = "application/json")
ProductObject getProduct(
@PathVariable("id") @ApiPathParam(description = "The id of the product") int id) {
return (ProductObject) productManager.getProduct(id);
}

@ApiMethod(produces = "application/json", description = "Creates new product")
@ApiResponseObject
@RequestMapping(method = RequestMethod.POST, value = "/product", consumes = "application/json")
void addProduct(@RequestBody @ApiBodyObject ProductObject product) {
productManager.createProduct(product);
}

@ApiMethod(produces = "application/json", description = "Updates existing product")
@ApiResponseObject
@RequestMapping(method = RequestMethod.PUT, value = "/product/{id}", consumes = "application/json")
void updateProduct(
@PathVariable("id") @ApiPathParam(description = "The id of the product") int id,
@RequestBody @ApiBodyObject ProductObject product) {

product.setId(id);
productManager.updateProduct(product);
}

@ApiMethod(produces = "application/json", description = "Gets products in given category")
@ApiResponseObject
@RequestMapping(method = RequestMethod.GET, value = "/category/{id}/products", produces = "application/json")
List<Product> getProductInCategory(
@PathVariable("id") @ApiPathParam(description = "The id of the category") int id,
@RequestParam(defaultValue = "0", required = false) @ApiQueryParam(name = "from", defaultvalue = "0", required = false) int from,
@RequestParam(defaultValue = "10", required = false) @ApiQueryParam(name = "to", defaultvalue = "10", required = false) int to) {

return productManager.getProductsInCategory(id, from, to);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
*
* You may not modify, use, reproduce, or distribute this software except in
* compliance with the terms of the License at:
* http://java.net/projects/javaeetutorial/pages/BerkeleyLicense
*/
package com.forest.micro.catalog.model;

import javax.xml.bind.annotation.XmlRootElement;

import org.jsondoc.core.annotation.ApiObject;
import org.jsondoc.core.annotation.ApiObjectField;

import com.forest.model.Category;

@ApiObject(name = "Category", description = "Category")
@XmlRootElement
public class CategoryObject extends Category {

private static final long serialVersionUID = -5400424750505982222L;

@ApiObjectField(description = "Category name", required = true)
protected String name;

@ApiObjectField(description = "Tags")
protected String tags;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
*
* You may not modify, use, reproduce, or distribute this software except in
* compliance with the terms of the License at:
* http://java.net/projects/javaeetutorial/pages/BerkeleyLicense
*/
package com.forest.micro.catalog.model;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.jsondoc.core.annotation.ApiObject;
import org.jsondoc.core.annotation.ApiObjectField;

import com.forest.model.Category;
import com.forest.model.Product;

@ApiObject (name="Product", description="Product")
@XmlRootElement
public class ProductObject extends Product {

private static final long serialVersionUID = -9109112921000514199L;


@ApiObjectField(description="Product name", required=true)
protected String name;

@ApiObjectField(description="Product price")
protected double price;

@ApiObjectField(description="Product description")
protected String description;

@XmlTransient
protected Category category;

@ApiObjectField(description="Category to add to")
protected Integer categoryId;

public Integer getCategoryId() {
return categoryId;
}

public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.forest.micro.catalog.wiring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.forest.usecase.catalog.AbstractBaseCategoryManager;
import com.forest.usecase.catalog.persistence.CategoryPersistence;


@Component
public class CategoryManager extends AbstractBaseCategoryManager {

@Autowired
private CategoryPersistence categoryPersistence;

@Override
protected CategoryPersistence getCategoryPersistence() {
return categoryPersistence;
}

}
Loading

0 comments on commit 0f8a0ce

Please sign in to comment.