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

@ -1,66 +1,66 @@
package com.baeldung.web; package com.baeldung.web;
import java.util.List; import java.util.List;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException; 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
private FooRepository repo; private FooRepository repo;
// API - read // API - read
@GetMapping("/foos/{id}") @GetMapping("/foos/{id}")
@Validated @Validated
public Foo findById(@PathVariable @Min(0) final long id) { public Foo findById(@PathVariable @Min(0) final long id) {
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();
} }
// API - write // API - write
@PutMapping("/foos/{id}") @PutMapping("/foos/{id}")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) { public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) {
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()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST," 'name' is required"); throw new ResponseStatusException(HttpStatus.BAD_REQUEST," 'name' is required");
} }
repo.save(foo); repo.save(foo);
} }
} }

View File

@ -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