작성
·
311
0
public void createEvent_Bad_Request_Empty_Input() throws Exception {
EventDto eventDto = EventDto.builder().build();
this.mockMvc.perform(post("/api/events/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(this.objectMapper.writeValueAsString(eventDto)))
.andExpect(status().isBadRequest());
package dlee.demorestapi.events;
import org.modelmapper.ModelMapper;
import org.modelmapper.internal.Errors;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.Valid;
import java.net.URI;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
@Controller
@RequestMapping(value="/api/events",produces = MediaTypes.HAL_JSON_VALUE)
public class EventController {
private final EventRepository eventRepository;
private final ModelMapper modelMapper;
public EventController(EventRepository eventRepository,ModelMapper modelMapper){
this.eventRepository = eventRepository;
this.modelMapper = modelMapper;
}
@PostMapping
public ResponseEntity createEvent(@RequestBody @Valid EventDto eventDto, Errors errors){ //JSON을 객체로 변환하는거니까 deserializationerrors
System.out.println(errors.getMessages() + "asdsadasds");
if(errors.hasErrors()){
return ResponseEntity.badRequest().build();
}
Event event = modelMapper.map(eventDto,Event.class);
Event newEvent = this.eventRepository.save(event);
URI createdUri = linkTo(EventController.class).slash(newEvent.getId()).toUri();
return ResponseEntity.created(createdUri).body(event);
}
}
package dlee.demorestapi.events;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
@Data @Builder @NoArgsConstructor @AllArgsConstructor
public class EventDto {
@NotEmpty
private String name;
@NotEmpty
private String description;
@NotNull
private LocalDateTime beginEnrollmentDateTime;
@NotNull
private LocalDateTime closeEnrollmentDateTime;
@NotNull
private LocalDateTime beginEventDateTime;
@NotNull
private LocalDateTime endEventDateTime;
private String location;
@Min(0)
private int basePrice;
@Min(0)
private int maxPrice;
@Min(0)
private int limitOfEnrollment;
}
답변 2
2
이미 해결 하셨을 수도 있지만 다른 분들을 위해 적어놓겠습니다.
먼저 @Vaild가 없어서 의존성을 추가했고,
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
위의 밤의멜로디님의 답변대로 해도 똑같이 201응답이 왔습니다.
인터넷에 찾아보니 org.hibernate.validator 추가하라는 내용이 있어서
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
추가하니 해결됬습니다.
참고한 글입니다
https://stackoverflow.com/questions/27886038/how-to-turn-on-annotation-driven-validation-in-spring-4/27903277#27903277
추가적으로
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
위에 2개 추가안하셔도 이거만 추가해도 됩니다
저걸로 import해도 똑같이 201 응답 떨어져요