Created
August 4, 2016 18:33
-
-
Save cristopher-rodrigues/36e030e2808c55c421b0dae48207f1a9 to your computer and use it in GitHub Desktop.
Generic Stack Spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BaseEntity | |
public abstract class BaseEntity<ID extends Serializable> | |
extends AbstractPersistable<ID>{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
public String toString(){ | |
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); | |
} | |
@Override | |
public boolean equals(Object obj){ | |
return EqualsBuilder.reflectionEquals(this, obj); | |
} | |
@Override | |
public void setId(ID id){ | |
super.setId(id); | |
} | |
@JsonIgnore | |
@Override | |
public boolean isNew(){ | |
return super.isNew(); | |
} | |
} | |
// RestServiceMap | |
@RequestMapping(consumes="application/json", produces="application/json") | |
public interface RestServiceMap { | |
} | |
// RestController | |
public abstract class RestController<T extends BaseEntity<ID>, ID extends Serializable> implements ServiceMap { | |
private final Logger LOGGER = Logger.getLogger(getClass()); | |
@Autowired | |
protected JpaRepository<T, ID> genericRepository; | |
@RequestMapping(method=RequestMethod.GET) | |
public List<T> findAll(){ | |
this.LOGGER.info("Requesting all records."); | |
return this.genericRepository.findAll(); | |
} | |
@RequestMapping(method=RequestMethod.POST) | |
public T create(@RequestBody @Valid T t, | |
BindingResult bindingResult) throws Exception { | |
if (bindingResult.hasErrors()) { | |
this.LOGGER.info(String.format("Error create the entity [%s].", t)); | |
throw new Exception("Validation Error"); | |
} | |
this.LOGGER.info(String.format("Creating the entity [%s].", t)); | |
return this.genericRepository.insert(t); | |
} | |
} | |
// ServicePath | |
public final class ServicePath { | |
public static final String ALL = "/**"; | |
public static final String ROOT_PATH = "/api"; | |
public static final String PUBLIC_ROOT_PATH = ROOT_PATH + "/public"; | |
public static final String PRIVATE_ROOT_PATH = ROOT_PATH + "/private"; | |
public static final String USER_PATH = PRIVATE_ROOT_PATH + "/users"; | |
} | |
// UserController | |
@RestController | |
@RequestMapping(path=ServicePath.CUSTOMER_PATH) | |
public class UserController extends RestController<User, String> { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment