upgrade katharsis 3 (#4429)

This commit is contained in:
Doha2012 2018-06-08 16:22:55 +00:00 committed by Grzegorz Piwowarek
parent 749611e314
commit d28ffe5982
9 changed files with 93 additions and 81 deletions

View File

@ -33,7 +33,7 @@
<dependency>
<groupId>io.katharsis</groupId>
<artifactId>katharsis-servlet</artifactId>
<artifactId>katharsis-spring</artifactId>
<version>${katharsis.version}</version>
<exclusions>
<exclusion>

View File

@ -1,10 +1,14 @@
package org.baeldung;
import io.katharsis.spring.boot.v3.KatharsisConfigV3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(KatharsisConfigV3.class)
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {

View File

@ -1,40 +0,0 @@
package org.baeldung.persistence.katharsis;
import io.katharsis.invoker.internal.legacy.KatharsisInvokerBuilder;
import io.katharsis.legacy.locator.JsonServiceLocator;
import io.katharsis.servlet.legacy.AbstractKatharsisFilter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;
@Component
public class JsonApiFilter extends AbstractKatharsisFilter implements BeanFactoryAware {
private static final String DEFAULT_RESOURCE_SEARCH_PACKAGE = "org.baeldung.persistence";
private static final String RESOURCE_DEFAULT_DOMAIN = "http://localhost:8080";
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
protected KatharsisInvokerBuilder createKatharsisInvokerBuilder() {
final KatharsisInvokerBuilder builder = new KatharsisInvokerBuilder();
builder.resourceSearchPackage(DEFAULT_RESOURCE_SEARCH_PACKAGE).resourceDefaultDomain(RESOURCE_DEFAULT_DOMAIN).jsonServiceLocator(new JsonServiceLocator() {
@Override
public <T> T getInstance(Class<T> clazz) {
return beanFactory.getBean(clazz);
}
});
return builder;
}
}

View File

@ -1,7 +1,9 @@
package org.baeldung.persistence.katharsis;
import io.katharsis.legacy.queryParams.QueryParams;
import io.katharsis.legacy.repository.ResourceRepository;
import io.katharsis.queryspec.QuerySpec;
import io.katharsis.repository.ResourceRepositoryV2;
import io.katharsis.resource.list.ResourceList;
import org.baeldung.persistence.dao.RoleRepository;
import org.baeldung.persistence.model.Role;
@ -9,23 +11,23 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RoleResourceRepository implements ResourceRepository<Role, Long> {
public class RoleResourceRepository implements ResourceRepositoryV2<Role, Long> {
@Autowired private RoleRepository roleRepository;
@Override
public Role findOne(Long id, QueryParams params) {
public Role findOne(Long id, QuerySpec querySpec) {
return roleRepository.findOne(id);
}
@Override
public Iterable<Role> findAll(QueryParams params) {
return roleRepository.findAll();
public ResourceList<Role> findAll(QuerySpec querySpec) {
return querySpec.apply(roleRepository.findAll());
}
@Override
public Iterable<Role> findAll(Iterable<Long> ids, QueryParams params) {
return roleRepository.findAll(ids);
public ResourceList<Role> findAll(Iterable<Long> ids, QuerySpec querySpec) {
return querySpec.apply(roleRepository.findAll(ids));
}
@Override
@ -38,4 +40,14 @@ public class RoleResourceRepository implements ResourceRepository<Role, Long> {
roleRepository.delete(id);
}
@Override
public Class<Role> getResourceClass() {
return Role.class;
}
@Override
public <S extends Role> S create(S entity) {
return save(entity);
}
}

View File

@ -1,7 +1,8 @@
package org.baeldung.persistence.katharsis;
import io.katharsis.legacy.queryParams.QueryParams;
import io.katharsis.legacy.repository.ResourceRepository;
import io.katharsis.queryspec.QuerySpec;
import io.katharsis.repository.ResourceRepositoryV2;
import io.katharsis.resource.list.ResourceList;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.model.User;
@ -9,24 +10,24 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserResourceRepository implements ResourceRepository<User, Long> {
public class UserResourceRepository implements ResourceRepositoryV2<User, Long> {
@Autowired
private UserRepository userRepository;
@Override
public User findOne(Long id, QueryParams params) {
public User findOne(Long id, QuerySpec querySpec) {
return userRepository.findOne(id);
}
@Override
public Iterable<User> findAll(QueryParams params) {
return userRepository.findAll();
public ResourceList<User> findAll(QuerySpec querySpec) {
return querySpec.apply(userRepository.findAll());
}
@Override
public Iterable<User> findAll(Iterable<Long> ids, QueryParams params) {
return userRepository.findAll(ids);
public ResourceList<User> findAll(Iterable<Long> ids, QuerySpec querySpec) {
return querySpec.apply(userRepository.findAll(ids));
}
@Override
@ -39,4 +40,14 @@ public class UserResourceRepository implements ResourceRepository<User, Long> {
userRepository.delete(id);
}
@Override
public Class<User> getResourceClass() {
return User.class;
}
@Override
public <S extends User> S create(S entity) {
return save(entity);
}
}

View File

@ -1,7 +1,8 @@
package org.baeldung.persistence.katharsis;
import io.katharsis.legacy.queryParams.QueryParams;
import io.katharsis.legacy.repository.RelationshipRepository;
import io.katharsis.queryspec.QuerySpec;
import io.katharsis.repository.RelationshipRepositoryV2;
import io.katharsis.resource.list.ResourceList;
import java.util.HashSet;
import java.util.Set;
@ -14,7 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserToRoleRelationshipRepository implements RelationshipRepository<User, Long, Role, Long> {
public class UserToRoleRelationshipRepository implements RelationshipRepositoryV2<User, Long, Role, Long> {
@Autowired
private UserRepository userRepository;
@ -52,14 +53,25 @@ public class UserToRoleRelationshipRepository implements RelationshipRepository<
}
@Override
public Role findOneTarget(Long sourceId, String fieldName, QueryParams QueryParams) {
public Role findOneTarget(Long sourceId, String fieldName, QuerySpec querySpec) {
// not for many-to-many
return null;
}
@Override
public Iterable<Role> findManyTargets(Long sourceId, String fieldName, QueryParams QueryParams) {
public ResourceList<Role> findManyTargets(Long sourceId, String fieldName, QuerySpec querySpec) {
final User user = userRepository.findOne(sourceId);
return user.getRoles();
return querySpec.apply(user.getRoles());
}
@Override
public Class<User> getSourceResourceClass() {
return User.class;
}
@Override
public Class<Role> getTargetResourceClass() {
return Role.class;
}
}

View File

@ -1,8 +1,8 @@
package org.baeldung.persistence.model;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiRelation;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import java.util.Set;
@ -25,7 +25,7 @@ public class Role {
private String name;
@ManyToMany(mappedBy = "roles")
@JsonApiToMany
@JsonApiRelation
private Set<User> users;
//
@ -66,23 +66,30 @@ public class Role {
@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
}
final Role other = (Role) obj;
if (id == null) {
if (other.id != null)
if (other.id != null) {
return false;
} else if (!id.equals(other.id))
}
} else if (!id.equals(other.id)) {
return false;
}
if (name == null) {
if (other.name != null)
if (other.name != null) {
return false;
} else if (!name.equals(other.name))
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}

View File

@ -1,9 +1,9 @@
package org.baeldung.persistence.model;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiIncludeByDefault;
import io.katharsis.resource.annotations.JsonApiRelation;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import io.katharsis.resource.annotations.SerializeType;
import java.util.Set;
@ -31,8 +31,7 @@ public class User {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
@JsonApiToMany
@JsonApiIncludeByDefault
@JsonApiRelation(serialize=SerializeType.EAGER)
private Set<Role> roles;
public User() {
@ -87,15 +86,19 @@ public class User {
@Override
public boolean equals(final Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
}
final User user = (User) obj;
if (!email.equals(user.email))
if (!email.equals(user.email)) {
return false;
}
return true;
}

View File

@ -6,4 +6,7 @@ spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
server.port=8082
server.context-path=/spring-katharsis
server.context-path=/spring-katharsis
katharsis.domainName=http://localhost:8082/spring-katharsis
katharsis.pathPrefix=/