spring-5-mvc (#1952)
This commit is contained in:
parent
fe841fe53b
commit
8f3039b292
@ -1,66 +1,66 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.baeldung.model.Foo;
|
||||
import com.baeldung.persistence.FooRepository;
|
||||
|
||||
@RestController("/foos")
|
||||
public class FooController {
|
||||
|
||||
@Autowired
|
||||
private FooRepository repo;
|
||||
|
||||
// API - read
|
||||
|
||||
@GetMapping("/foos/{id}")
|
||||
@Validated
|
||||
public Foo findById(@PathVariable @Min(0) final long id) {
|
||||
return repo.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Foo> findAll() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@GetMapping(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();
|
||||
}
|
||||
|
||||
// API - write
|
||||
|
||||
@PutMapping("/foos/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {
|
||||
return foo;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create( @RequestBody final Foo foo) {
|
||||
if (null == foo || null == foo.getName()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST," 'name' is required");
|
||||
}
|
||||
repo.save(foo);
|
||||
}
|
||||
}
|
||||
package com.baeldung.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.baeldung.model.Foo;
|
||||
import com.baeldung.persistence.FooRepository;
|
||||
|
||||
@RestController
|
||||
public class FooController {
|
||||
|
||||
@Autowired
|
||||
private FooRepository repo;
|
||||
|
||||
// API - read
|
||||
|
||||
@GetMapping("/foos/{id}")
|
||||
@Validated
|
||||
public Foo findById(@PathVariable @Min(0) final long id) {
|
||||
return repo.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
@GetMapping("/foos")
|
||||
public List<Foo> findAll() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
// API - write
|
||||
|
||||
@PutMapping("/foos/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {
|
||||
return foo;
|
||||
}
|
||||
|
||||
@PostMapping("/foos")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create( @RequestBody final Foo foo) {
|
||||
if (null == foo || null == foo.getName()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST," 'name' is required");
|
||||
}
|
||||
repo.save(foo);
|
||||
}
|
||||
}
|
@ -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 |
Loading…
x
Reference in New Issue
Block a user