Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 34 additions & 130 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,175 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
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>

<groupId>nl.michelbijnen.jsonapi</groupId>
<artifactId>json-api</artifactId>
<version>1.6.0</version>
<packaging>jar</packaging>

<name>nl.michelbijnen.jsonapi:json-api-converter</name>
<name>json-api-converter</name>
<description>Converter for DTOs to json:api</description>
<url>https://github.com/MieskeB/json-api-spring-boot</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<relativePath/>
</parent>

<properties>
<java.version>11</java.version>
<maven.compiler.release>8</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<developers>
<developer>
<id>mbijnen</id>
<name>Michel Bijnen</name>
<email>info@michelbijnen.nl</email>
<url>https://www.michelbijnen.nl/</url>
</developer>
<developer>
<id>sdeboer</id>
<name>Sander de Boer</name>
<email>info@sanderdeboer.me</email>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/MieskeB/json-api-spring-boot.git</connection>
<developerConnection>scm:git:ssh://github.com:MieskeB/json-api-spring-boot.git</developerConnection>
<url>https://github.com/MieskeB/json-api-spring-boot</url>
</scm>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Jackson JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.21.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.21.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.21.0</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.9.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
<autoPublish>true</autoPublish>
<waitUntil>published</waitUntil>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<configuration>
<sourceEncoding>UTF-8</sourceEncoding>
</configuration>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>${maven.compiler.release}</release>
<release>${java.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
21 changes: 21 additions & 0 deletions src/main/java/nl/michelbijnen/jsonapi/models/Category.java
Original file line number Diff line number Diff line change
@@ -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; }
}
40 changes: 40 additions & 0 deletions src/main/java/nl/michelbijnen/jsonapi/models/Product.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}