Skip to content

Commit 96aca9f

Browse files
committed
json api
1 parent 671d78b commit 96aca9f

12 files changed

Lines changed: 403 additions & 0 deletions

File tree

‎spring-katharsis/.classpath‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="output" path="target/classes"/>
31+
</classpath>

‎spring-katharsis/.project‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-katharsis</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.m2e.core.maven2Builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
26+
<nature>org.eclipse.jdt.core.javanature</nature>
27+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
28+
</natures>
29+
</projectDescription>

‎spring-katharsis/.springBeans‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beansProjectDescription>
3+
<version>1</version>
4+
<pluginVersion><![CDATA[3.2.0.201303060654-RELEASE]]></pluginVersion>
5+
<configSuffixes>
6+
<configSuffix><![CDATA[xml]]></configSuffix>
7+
</configSuffixes>
8+
<enableImports><![CDATA[false]]></enableImports>
9+
<configs>
10+
</configs>
11+
<configSets>
12+
</configSets>
13+
</beansProjectDescription>

‎spring-katharsis/pom.xml‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.springframework.samples</groupId>
4+
<artifactId>spring-katharsis</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>1.2.6.RELEASE</version>
11+
</parent>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-web</artifactId>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-data-jpa</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>com.h2database</groupId>
26+
<artifactId>h2</artifactId>
27+
</dependency>
28+
29+
<!-- json api -->
30+
31+
<dependency>
32+
<groupId>io.katharsis</groupId>
33+
<artifactId>katharsis-servlet</artifactId>
34+
<version>1.0.0</version>
35+
</dependency>
36+
37+
<!-- test -->
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.jayway.restassured</groupId>
47+
<artifactId>rest-assured</artifactId>
48+
<version>2.4.0</version>
49+
<scope>test</scope>
50+
<exclusions>
51+
<exclusion>
52+
<artifactId>commons-logging</artifactId>
53+
<groupId>commons-logging</groupId>
54+
</exclusion>
55+
</exclusions>
56+
</dependency>
57+
58+
</dependencies>
59+
60+
61+
<properties>
62+
<java.version>1.8</java.version>
63+
</properties>
64+
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
12+
}
13+
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung;
2+
3+
import javax.annotation.PostConstruct;
4+
5+
import org.baeldung.persistence.dao.UserRepository;
6+
import org.baeldung.persistence.model.User;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class Setup {
12+
13+
@Autowired
14+
private UserRepository userRepository;
15+
16+
@PostConstruct
17+
private void setupData() {
18+
final User user = new User();
19+
user.setFirstName("john");
20+
user.setLastName("doe");
21+
user.setEmail("john@test.com");
22+
userRepository.save(user);
23+
}
24+
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.persistence.dao;
2+
3+
import org.baeldung.persistence.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface UserRepository extends JpaRepository<User, Long> {
7+
8+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.baeldung.persistence.katharsis;
2+
3+
import io.katharsis.invoker.KatharsisInvokerBuilder;
4+
import io.katharsis.locator.JsonServiceLocator;
5+
import io.katharsis.servlet.AbstractKatharsisFilter;
6+
7+
import org.springframework.beans.BeansException;
8+
import org.springframework.beans.factory.BeanFactory;
9+
import org.springframework.beans.factory.BeanFactoryAware;
10+
import org.springframework.stereotype.Component;
11+
12+
@Component
13+
public class JsonApiFilter extends AbstractKatharsisFilter implements BeanFactoryAware {
14+
15+
private static final String DEFAULT_RESOURCE_SEARCH_PACKAGE = "org.baeldung.persistence";
16+
17+
private static final String RESOURCE_DEFAULT_DOMAIN = "http://localhost:8080";
18+
19+
private BeanFactory beanFactory;
20+
21+
@Override
22+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
23+
this.beanFactory = beanFactory;
24+
}
25+
26+
@Override
27+
protected KatharsisInvokerBuilder createKatharsisInvokerBuilder() {
28+
final KatharsisInvokerBuilder builder = new KatharsisInvokerBuilder();
29+
30+
builder.resourceSearchPackage(DEFAULT_RESOURCE_SEARCH_PACKAGE).resourceDefaultDomain(RESOURCE_DEFAULT_DOMAIN).jsonServiceLocator(new JsonServiceLocator() {
31+
@Override
32+
public <T> T getInstance(Class<T> clazz) {
33+
return beanFactory.getBean(clazz);
34+
}
35+
});
36+
37+
return builder;
38+
}
39+
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.baeldung.persistence.katharsis;
2+
3+
import io.katharsis.queryParams.RequestParams;
4+
import io.katharsis.repository.ResourceRepository;
5+
6+
import org.baeldung.persistence.dao.UserRepository;
7+
import org.baeldung.persistence.model.User;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Component;
10+
11+
@Component
12+
public class UserResourceRepository implements ResourceRepository<User, Long> {
13+
14+
@Autowired
15+
private UserRepository userRepository;
16+
17+
@Override
18+
public User findOne(Long id, RequestParams params) {
19+
return userRepository.findOne(id);
20+
}
21+
22+
@Override
23+
public Iterable<User> findAll(RequestParams params) {
24+
return userRepository.findAll();
25+
}
26+
27+
@Override
28+
public Iterable<User> findAll(Iterable<Long> ids, RequestParams params) {
29+
return userRepository.findAll(ids);
30+
}
31+
32+
@Override
33+
public <S extends User> S save(S entity) {
34+
return userRepository.save(entity);
35+
}
36+
37+
@Override
38+
public void delete(Long id) {
39+
userRepository.delete(id);
40+
}
41+
42+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.baeldung.persistence.model;
2+
3+
import io.katharsis.resource.annotations.JsonApiId;
4+
import io.katharsis.resource.annotations.JsonApiResource;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
11+
@Entity
12+
@JsonApiResource(type = "users")
13+
public class User {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
@JsonApiId
18+
private Long id;
19+
20+
private String firstName;
21+
22+
private String lastName;
23+
24+
private String email;
25+
26+
public User() {
27+
super();
28+
}
29+
30+
public Long getId() {
31+
return id;
32+
}
33+
34+
public void setId(final Long id) {
35+
this.id = id;
36+
}
37+
38+
public String getFirstName() {
39+
return firstName;
40+
}
41+
42+
public void setFirstName(final String firstName) {
43+
this.firstName = firstName;
44+
}
45+
46+
public String getLastName() {
47+
return lastName;
48+
}
49+
50+
public void setLastName(final String lastName) {
51+
this.lastName = lastName;
52+
}
53+
54+
public String getEmail() {
55+
return email;
56+
}
57+
58+
public void setEmail(final String username) {
59+
email = username;
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
final int prime = 31;
65+
int result = 1;
66+
result = prime * result + (email == null ? 0 : email.hashCode());
67+
return result;
68+
}
69+
70+
@Override
71+
public boolean equals(final Object obj) {
72+
if (this == obj)
73+
return true;
74+
if (obj == null)
75+
return false;
76+
if (getClass() != obj.getClass())
77+
return false;
78+
final User user = (User) obj;
79+
if (!email.equals(user.email))
80+
return false;
81+
return true;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
final StringBuilder builder = new StringBuilder();
87+
builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]");
88+
return builder.toString();
89+
}
90+
91+
}

0 commit comments

Comments
 (0)