Skip to content

Commit

Permalink
feat: validate rest input using bean validation and add a mapper for …
Browse files Browse the repository at this point in the history
…constraint violations
  • Loading branch information
mburri committed Dec 8, 2023
1 parent 568c358 commit 60d61f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(ValidationExceptionMapper.class);
resources.add(NotFoundExceptionMapper.class);
resources.add(UncaughtExceptionMapper.class);
resources.add(ConstraintViolationExceptionMapper.class);

// health
resources.add(HealthCheck.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ch.mobi.itc.mobiliar.rest.exceptions;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.stream.Collectors;

public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

@Override
public Response toResponse(ConstraintViolationException e) {
final String message = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.distinct() // since we use CDI and JAX-RS, there is potential that the validation kicks in twice.
// calling distinct on the list of messages makes sure, that each message is included only once.
.collect(Collectors.joining("\n"));
return Response.status(Response.Status.BAD_REQUEST).entity(new ExceptionDto(message)).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.mobi.itc.mobiliar.rest.tags.boundary;

import ch.mobi.itc.mobiliar.rest.dtos.TagDTO;
import ch.mobi.itc.mobiliar.rest.exceptions.ExceptionDto;
import ch.puzzle.itc.mobiliar.business.property.boundary.AddTagUseCase;
import ch.puzzle.itc.mobiliar.business.property.boundary.ListTagsUseCase;
import ch.puzzle.itc.mobiliar.business.property.boundary.RemoveTagUseCase;
Expand All @@ -10,15 +9,13 @@
import ch.puzzle.itc.mobiliar.common.exception.ValidationException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.Objects;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.CREATED;
Expand All @@ -44,16 +41,14 @@ public class TagsRest {

@ApiOperation(value = "Gets all tags")
public Response getAllTags() {
return Response
.status(OK)
.entity(listTagsUseCase.get())
.build();
return Response.status(OK).entity(listTagsUseCase.get()).build();
}

@POST
@ApiOperation(value = "Adds one tag")
public Response addOneTag(TagDTO tagDTO) throws ValidationException {
if(Objects.isNull(tagDTO)) throw new ValidationException("Tag must not be null.");
public Response addOneTag(
@NotNull(message = "Tag must not be null.") TagDTO tagDTO) throws ValidationException {

TagCommand tagCommand = new TagCommand(tagDTO.getName());
PropertyTagEntity newTag = addTagUseCase.addTag(tagCommand);
return Response
Expand Down

0 comments on commit 60d61f1

Please sign in to comment.