BAEL-738 (#1456) PUT and PATCH
* BAEL-724 code for put/patch article * BAEL-724 fix typo
This commit is contained in:
parent
b4b2ad3edb
commit
b95a014c8a
@ -0,0 +1,14 @@
|
|||||||
|
package org.baeldung.repository;
|
||||||
|
|
||||||
|
import org.baeldung.web.dto.HeavyResource;
|
||||||
|
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
|
||||||
|
|
||||||
|
public class HeavyResourceRepository {
|
||||||
|
|
||||||
|
public void save(HeavyResource heavyResource) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(HeavyResourceAddressPartialUpdate partialUpdate) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package org.baeldung.web.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import org.baeldung.repository.HeavyResourceRepository;
|
||||||
|
import org.baeldung.web.dto.HeavyResource;
|
||||||
|
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HeavyResourceController {
|
||||||
|
|
||||||
|
private HeavyResourceRepository heavyResourceRepository = new HeavyResourceRepository();
|
||||||
|
|
||||||
|
@RequestMapping(value = "/heavy", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public ResponseEntity<?> saveResource(@RequestBody HeavyResource heavyResource) {
|
||||||
|
heavyResourceRepository.save(heavyResource);
|
||||||
|
return ResponseEntity.ok("resource saved");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/heavy", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public ResponseEntity<?> partialUpdateName(@RequestBody HeavyResourceAddressPartialUpdate partialUpdate) {
|
||||||
|
heavyResourceRepository.save(partialUpdate);
|
||||||
|
return ResponseEntity.ok("resource address updated");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package org.baeldung.web.dto;
|
||||||
|
|
||||||
|
|
||||||
|
public class HeavyResource {
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private String surname;
|
||||||
|
private Integer age;
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
|
||||||
|
public HeavyResource() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeavyResource(Integer id, String name, String surname, Integer age, String address) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.surname = surname;
|
||||||
|
this.age = age;
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package org.baeldung.web.dto;
|
||||||
|
|
||||||
|
|
||||||
|
public class HeavyResourceAddressPartialUpdate {
|
||||||
|
private Integer id;
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
public HeavyResourceAddressPartialUpdate() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public HeavyResourceAddressPartialUpdate(Integer id, String address) {
|
||||||
|
this.id = id;
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package org.baeldung.web.controller;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.baeldung.config.WebConfig;
|
||||||
|
import org.baeldung.web.dto.HeavyResource;
|
||||||
|
import org.baeldung.web.dto.HeavyResourceAddressPartialUpdate;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = WebConfig.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
public class HeavyResourceControllerTest {
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext webApplicationContext;
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Exception {
|
||||||
|
mockMvc.perform(put("/heavy")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
.content(objectMapper.writeValueAsString(new HeavyResource(1, "Tom", "Jackson", 12, "heaven street")))
|
||||||
|
).andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNewAddressOfResource_whenExecutePutRequest_thenUpdateResourcePartially() throws Exception {
|
||||||
|
mockMvc.perform(patch("/heavy")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
.content(objectMapper.writeValueAsString(new HeavyResourceAddressPartialUpdate(1, "5th avenue")))
|
||||||
|
).andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user