spring-5-mvc (#1952)

This commit is contained in:
asif-anwar 2017-05-29 20:58:25 +04:00 committed by Eugen
parent fe841fe53b
commit 8f3039b292
3 changed files with 69 additions and 69 deletions

View File

@ -22,7 +22,7 @@ import org.springframework.web.server.ResponseStatusException;
import com.baeldung.model.Foo;
import com.baeldung.persistence.FooRepository;
@RestController("/foos")
@RestController
public class FooController {
@Autowired
@ -36,12 +36,12 @@ public class FooController {
return repo.findById(id).orElse(null);
}
@GetMapping
@GetMapping("/foos")
public List<Foo> findAll() {
return repo.findAll();
}
@GetMapping(params = { "page", "size" })
@GetMapping( value="/foos", params = { "page", "size" })
@Validated
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
return repo.findAll(PageRequest.of(page, size)).getContent();
@ -55,7 +55,7 @@ public class FooController {
return foo;
}
@PostMapping
@PostMapping("/foos")
@ResponseStatus(HttpStatus.CREATED)
public void create( @RequestBody final Foo foo) {
if (null == foo || null == foo.getName()) {

View File

@ -16,14 +16,14 @@ public class LiveTest {
@Test
public void givenUser_whenPostWithNullName_then400BadRequest() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceWithNullName()).post(APP_ROOT + "/user");
public void givenUser_whenResourceCreatedWithNullName_then400BadRequest() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceWithNullName()).post(APP_ROOT + "/foos");
assertEquals(400, response.getStatusCode());
}
@Test
public void givenUser_whenResourceCreated_then201Created() {
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceString()).post(APP_ROOT + "/user");
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceString()).post(APP_ROOT + "/foos");
assertEquals(201, response.getStatusCode());
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB