From 1d10cab7e6fc2ba998d747b10d17d0e92d03e98f Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Fri, 1 Mar 2024 11:56:46 +0100 Subject: [PATCH] Modification des @ pour enlever warnings --- .../sae/controllers/Controller.java | 82 ------------------- .../sae/exceptions/ValidationHandler.java | 15 ++-- 2 files changed, 6 insertions(+), 91 deletions(-) delete mode 100644 SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java deleted file mode 100644 index 4315901..0000000 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/Controller.java +++ /dev/null @@ -1,82 +0,0 @@ -package fr.iut.sciencequest.sae.controllers; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.hateoas.CollectionModel; -import org.springframework.hateoas.EntityModel; -import org.springframework.hateoas.Link; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; - -public class Controller { - protected final int PAGE_SIZE = 1; - - protected EntityModel getSelfLinkEntityModel(T entity, String method, Object... args) { - Class[] argTypes = new Class[args.length]; - for (int i = 0; i < args.length; i++) { - argTypes[i] = args[i].getClass(); - } - - try { - Link selfLink = linkTo(this.getClass(), this.getClass().getMethod(method, argTypes), args).withSelfRel(); - return EntityModel.of(entity, selfLink); - } catch (NoSuchMethodException e) { - System.err.println("Method doesn't exist"); - return null; - } - } - - protected CollectionModel getSelfLinkCollectionModel(Iterable entities, String method, Object... args) { - Class[] argTypes = new Class[args.length]; - for (int i = 0; i < args.length; i++) { - argTypes[i] = args[i].getClass(); - } - - try { - Link selfLink = linkTo(this.getClass(), this.getClass().getMethod(method, argTypes), args).withSelfRel(); - return CollectionModel.of(entities, selfLink); - } catch (NoSuchMethodException e) { - System.err.println("Method doesn't exist"); - return null; - } - } - - - protected CollectionModel> getPageableCollectionModel(Page pagedResult, Integer page, String method, Object... args) { - try { - List> entities = pagedResult.map(EntityModel::of).toList(); - - Class[] argTypes = new Class[args.length+1]; - argTypes[0] = Pageable.class; - for (int i = 1; i < args.length; i++) { - argTypes[i] = args[i].getClass(); - } - - Method finalMethod = this.getClass().getMethod(method, argTypes); - - List selfObj = new ArrayList<>(List.of(args)); selfObj.add(page); - Link selfLink = linkTo(this.getClass(), finalMethod, selfObj.toArray()).withSelfRel().expand(page); - - CollectionModel> result = CollectionModel.of(entities, selfLink); - - if (pagedResult.hasPrevious()) { - List previousObj = new ArrayList<>(List.of(args)); previousObj.add(pagedResult.previousPageable().getPageNumber()); - result.add(linkTo(this.getClass(), finalMethod, previousObj.toArray()).withRel("previous")); - } - - if (pagedResult.hasNext()) { - List nextObj = new ArrayList<>(List.of(args)); nextObj.add(pagedResult.nextPageable().getPageNumber()); - result.add(linkTo(this.getClass(), finalMethod, nextObj.toArray()).withRel("next")); - } - - return result; - } catch(NoSuchMethodException e) { - System.err.println(e.getMessage()); - return null; - } - } -} diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/ValidationHandler.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/ValidationHandler.java index e72da29..74bd2fe 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/ValidationHandler.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/ValidationHandler.java @@ -1,29 +1,26 @@ package fr.iut.sciencequest.sae.exceptions; -import java.util.HashMap; -import java.util.Map; - +import jakarta.annotation.Nullable; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; -import org.springframework.lang.Nullable; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; +import java.util.HashMap; +import java.util.Map; + @ControllerAdvice public class ValidationHandler extends ResponseEntityExceptionHandler { - - @Nullable @Override @ResponseStatus(HttpStatus.BAD_REQUEST) - public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) { + public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nullable HttpHeaders headers, @Nullable HttpStatusCode status, @Nullable WebRequest request) { Map errors = new HashMap<>(); ex.getBindingResult().getAllErrors().forEach((error) ->{ @@ -31,6 +28,6 @@ public class ValidationHandler extends ResponseEntityExceptionHandler { String message = error.getDefaultMessage(); errors.put(fieldName, message); }); - return new ResponseEntity(errors, HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); } }