spring-5-mvc (#1952)
This commit is contained in:
parent
fe841fe53b
commit
8f3039b292
@ -22,7 +22,7 @@ import org.springframework.web.server.ResponseStatusException;
|
|||||||
import com.baeldung.model.Foo;
|
import com.baeldung.model.Foo;
|
||||||
import com.baeldung.persistence.FooRepository;
|
import com.baeldung.persistence.FooRepository;
|
||||||
|
|
||||||
@RestController("/foos")
|
@RestController
|
||||||
public class FooController {
|
public class FooController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -36,12 +36,12 @@ public class FooController {
|
|||||||
return repo.findById(id).orElse(null);
|
return repo.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping("/foos")
|
||||||
public List<Foo> findAll() {
|
public List<Foo> findAll() {
|
||||||
return repo.findAll();
|
return repo.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(params = { "page", "size" })
|
@GetMapping( value="/foos", params = { "page", "size" })
|
||||||
@Validated
|
@Validated
|
||||||
public List<Foo> findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) {
|
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();
|
return repo.findAll(PageRequest.of(page, size)).getContent();
|
||||||
@ -55,7 +55,7 @@ public class FooController {
|
|||||||
return foo;
|
return foo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping("/foos")
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public void create( @RequestBody final Foo foo) {
|
public void create( @RequestBody final Foo foo) {
|
||||||
if (null == foo || null == foo.getName()) {
|
if (null == foo || null == foo.getName()) {
|
||||||
|
@ -16,14 +16,14 @@ public class LiveTest {
|
|||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUser_whenPostWithNullName_then400BadRequest() {
|
public void givenUser_whenResourceCreatedWithNullName_then400BadRequest() {
|
||||||
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceWithNullName()).post(APP_ROOT + "/user");
|
final Response response = givenAuth("user", "pass").contentType(MediaType.APPLICATION_JSON.toString()).body(resourceWithNullName()).post(APP_ROOT + "/foos");
|
||||||
assertEquals(400, response.getStatusCode());
|
assertEquals(400, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUser_whenResourceCreated_then201Created() {
|
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());
|
assertEquals(201, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 22 KiB |
Loading…
x
Reference in New Issue
Block a user