Modification des @ pour enlever warnings

Springboot
Alix JEUDI--LEMOINE 1 year ago
parent 56be28929f
commit 1d10cab7e6

@ -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 <T> EntityModel<T> 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 <T> CollectionModel<T> getSelfLinkCollectionModel(Iterable<T> 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 <T> CollectionModel<EntityModel<T>> getPageableCollectionModel(Page<T> pagedResult, Integer page, String method, Object... args) {
try {
List<EntityModel<T>> 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<Object> selfObj = new ArrayList<>(List.of(args)); selfObj.add(page);
Link selfLink = linkTo(this.getClass(), finalMethod, selfObj.toArray()).withSelfRel().expand(page);
CollectionModel<EntityModel<T>> result = CollectionModel.of(entities, selfLink);
if (pagedResult.hasPrevious()) {
List<Object> 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<Object> 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;
}
}
}

@ -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<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nullable HttpHeaders headers, @Nullable HttpStatusCode status, @Nullable WebRequest request) {
Map<String, String> 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<Object>(errors, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
}

Loading…
Cancel
Save