Skip to content

Instantly share code, notes, and snippets.

@LidiaKovac
Created February 9, 2024 09:02
Show Gist options
  • Save LidiaKovac/738dcd0e16b626a3ed1813a45b700900 to your computer and use it in GitHub Desktop.
Save LidiaKovac/738dcd0e16b626a3ed1813a45b700900 to your computer and use it in GitHub Desktop.
JAVA SPRING INTERFACES (v0.1)
package ...;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import example.example.exceptions.BadRequestException;
import example.example.exceptions.ElementNotFoundException;
import java.util.List;
import java.util.UUID;
public interface IController<T, D, B> {
/*
* T = The class on which the Service will be using (example: Post, User)
* D = The DTO for newly created entities
* B = The DTO for editing entities
*
* Implementation example:
* public class UserController implements IController<User, NewUserDTO, EditUserDTO> {
*/
/* I don't add Controller annotations like @GetMapping because those require the class to be a Bean,
I do, however, add all url params, query params and validation here, since they are all similar in
all Controllers
*/
public T findById(@PathVariable UUID id) throws ElementNotFoundException;
public List<T> find(@RequestParam(required = false) String name) throws Exception;
public void findByIdAndDelete(@PathVariable UUID id) throws ElementNotFoundException;
public T findByIdAndUpdate(@PathVariable UUID id, @RequestBody @Validated B body, BindingResult validation) throws Exception;
public T create(@RequestBody @Validated D body, BindingResult validation) throws BadRequestException;
}
package ...;
import example.example.exceptions.BadRequestException;
import example.example.exceptions.ElementNotFoundException;
import java.util.List;
import java.util.UUID;
public interface IService<T, D, B> {
/*
* T = The class on which the Service will be using (example: Post, User)
* D = The DTO for newly created entities
* B = The DTO for editing entities
*
* Implementation example:
* public class UserService implements IService<User, NewUserDTO, EditUserDTO> {
*/
public T save(D obj) throws BadRequestException;
public T findById(UUID id) throws ElementNotFoundException;
public List<T> find();
public T findByIdAndUpdate(UUID id, B obj) throws Exception;
public void findByIdAndDelete(UUID id) throws ElementNotFoundException;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment