diff --git a/pom.xml b/pom.xml index dd6ed39..8d46f1d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,9 @@ + 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"> + 4.0.0 nl.michelbijnen.jsonapi @@ -9,167 +11,69 @@ 1.6.0 jar - nl.michelbijnen.jsonapi:json-api-converter + json-api-converter Converter for DTOs to json:api - https://github.com/MieskeB/json-api-spring-boot + + + org.springframework.boot + spring-boot-starter-parent + 3.3.1 + + 11 - 8 UTF-8 - - - MIT License - http://www.opensource.org/licenses/mit-license.php - - - - - - mbijnen - Michel Bijnen - info@michelbijnen.nl - https://www.michelbijnen.nl/ - - - sdeboer - Sander de Boer - info@sanderdeboer.me - - - - - scm:git:git://github.com/MieskeB/json-api-spring-boot.git - scm:git:ssh://github.com:MieskeB/json-api-spring-boot.git - https://github.com/MieskeB/json-api-spring-boot - - - + + + org.springframework.boot + spring-boot-starter-web + + + com.fasterxml.jackson.core jackson-databind - 2.21.0 - com.fasterxml.jackson.datatype jackson-datatype-jsr310 - 2.21.0 - + + - junit - junit - 4.13.2 + org.springframework.boot + spring-boot-starter-test test + + + com.vaadin.external.google + android-json + + - - org.mockito - mockito-core - 5.21.0 + junit + junit + 4.13.2 test - - org.sonatype.central - central-publishing-maven-plugin - 0.9.0 - true - - central - true - published - - - - org.apache.maven.plugins - maven-source-plugin - 3.3.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.6.3 - - - attach-javadocs - - jar - - - - - - org.apache.maven.plugins - maven-gpg-plugin - 3.2.4 - - - sign-artifacts - verify - - sign - - - - - - org.eluder.coveralls - coveralls-maven-plugin - 4.3.0 - - UTF-8 - - - - javax.xml.bind - jaxb-api - 2.3.1 - - - - - org.jacoco - jacoco-maven-plugin - 0.8.14 - - - prepare-agent - - prepare-agent - - - - org.apache.maven.plugins maven-compiler-plugin 3.11.0 - ${maven.compiler.release} + ${java.version} - - org.apache.maven.plugins - maven-surefire-plugin - 3.2.5 - - + \ No newline at end of file diff --git a/src/main/java/nl/michelbijnen/jsonapi/example/DemoApplication.java b/src/main/java/nl/michelbijnen/jsonapi/example/DemoApplication.java new file mode 100644 index 0000000..06d5f96 --- /dev/null +++ b/src/main/java/nl/michelbijnen/jsonapi/example/DemoApplication.java @@ -0,0 +1,11 @@ +package nl.michelbijnen.jsonapi.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/src/main/java/nl/michelbijnen/jsonapi/example/ProductController.java b/src/main/java/nl/michelbijnen/jsonapi/example/ProductController.java new file mode 100644 index 0000000..2ee9e2e --- /dev/null +++ b/src/main/java/nl/michelbijnen/jsonapi/example/ProductController.java @@ -0,0 +1,23 @@ +package nl.michelbijnen.jsonapi.example; + +import nl.michelbijnen.jsonapi.models.Category; +import nl.michelbijnen.jsonapi.models.Product; +import nl.michelbijnen.jsonapi.parser.JsonApiConverter; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/products") +public class ProductController { + + @GetMapping("/{id}") + public ResponseEntity getProduct(@PathVariable String id) { + Category category = new Category("1", "Electronics"); + Product product = new Product(id, "Laptop", 1200.0, category); + String json = JsonApiConverter.convert(product, 2); + return ResponseEntity.ok(json); + } +} diff --git a/src/main/java/nl/michelbijnen/jsonapi/models/Category.java b/src/main/java/nl/michelbijnen/jsonapi/models/Category.java new file mode 100644 index 0000000..4c547d4 --- /dev/null +++ b/src/main/java/nl/michelbijnen/jsonapi/models/Category.java @@ -0,0 +1,21 @@ +package nl.michelbijnen.jsonapi.models; + +import nl.michelbijnen.jsonapi.annotation.JsonApiObject; +import nl.michelbijnen.jsonapi.annotation.JsonApiProperty; +import nl.michelbijnen.jsonapi.generator.JsonApiDtoExtendable; + +@JsonApiObject("Category") +public class Category extends JsonApiDtoExtendable { + + @JsonApiProperty + private String title; + + public Category(String id, String title) { + this.setId(id); + this.title = title; + this.generate("/category", "/categories"); + } + + public String getTitle() { return title; } + public void setTitle(String title) { this.title = title; } +} diff --git a/src/main/java/nl/michelbijnen/jsonapi/models/Product.java b/src/main/java/nl/michelbijnen/jsonapi/models/Product.java new file mode 100644 index 0000000..fd10e7e --- /dev/null +++ b/src/main/java/nl/michelbijnen/jsonapi/models/Product.java @@ -0,0 +1,40 @@ +package nl.michelbijnen.jsonapi.models; + +import nl.michelbijnen.jsonapi.annotation.JsonApiObject; +import nl.michelbijnen.jsonapi.annotation.JsonApiProperty; +import nl.michelbijnen.jsonapi.annotation.JsonApiRelation; +import nl.michelbijnen.jsonapi.generator.JsonApiDtoExtendable; + +@JsonApiObject("Product") +public class Product extends JsonApiDtoExtendable { + + @JsonApiProperty + private String name; + + @JsonApiProperty + private Double price; + + @JsonApiRelation("category") + private Category category; + + public Product(String id, String name, Double price,Category category) { + this.setId(id); + this.name = name; + this.price = price; + this.generate("/product", "/products"); + this.category = category; + } + + public String getName() { return name; } + public void setName(String name) { this.name = name; } + public Double getPrice() { return price; } + public void setPrice(Double price) { this.price = price; } + + public Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } +} diff --git a/src/test/java/nl/michelbijnen/jsonapi/test/ProductControllerTest.java b/src/test/java/nl/michelbijnen/jsonapi/test/ProductControllerTest.java new file mode 100644 index 0000000..fe78aa1 --- /dev/null +++ b/src/test/java/nl/michelbijnen/jsonapi/test/ProductControllerTest.java @@ -0,0 +1,29 @@ +package nl.michelbijnen.jsonapi.test; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@SpringBootTest(classes = nl.michelbijnen.jsonapi.example.DemoApplication.class) +@AutoConfigureMockMvc +class ProductControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + void testGetProductReturnsOk() throws Exception { + mvc.perform(get("/products/1").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.data.attributes.name").value("Laptop")) + .andExpect(jsonPath("$.data.relationships.category").exists()); + } +}