Merge pull request #6743 from eugenp/BAEL-2722-V2

move json resttemplate code
This commit is contained in:
Loredana Crusoveanu 2019-04-21 13:09:06 +03:00 committed by GitHub
commit 4fd430d723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package org.baeldung.resttemplate.web.controller;
import javax.servlet.http.HttpServletResponse;
import org.baeldung.resttemplate.web.dto.Person;
import org.baeldung.resttemplate.web.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@RestController
public class PersonAPI {
@Autowired
private PersonService personService;
@GetMapping("/")
public String home() {
return "Spring boot is working!";
}
@PostMapping(value = "/createPerson", consumes = "application/json", produces = "application/json")
public Person createPerson(@RequestBody Person person) {
return personService.saveUpdatePerson(person);
}
@PostMapping(value = "/updatePerson", consumes = "application/json", produces = "application/json")
public Person updatePerson(@RequestBody Person person, HttpServletResponse response) {
response.setHeader("Location", ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/findPerson/" + person.getId())
.toUriString());
return personService.saveUpdatePerson(person);
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.resttemplate.web.dto;
public class Person {
private Integer id;
private String name;
public Person() {
}
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
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;
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.resttemplate.web.service;
import org.baeldung.resttemplate.web.dto.Person;
public interface PersonService {
public Person saveUpdatePerson(Person person);
public Person findPersonById(Integer id);
}

View File

@ -0,0 +1,19 @@
package org.baeldung.resttemplate.web.service;
import org.baeldung.resttemplate.web.dto.Person;
import org.springframework.stereotype.Component;
@Component
public class PersonServiceImpl implements PersonService {
@Override
public Person saveUpdatePerson(Person person) {
return person;
}
@Override
public Person findPersonById(Integer id) {
return new Person(id, "John");
}
}

View File

@ -0,0 +1,96 @@
package org.baeldung.resttemplate;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.URI;
import org.baeldung.resttemplate.web.dto.Person;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RestTemplateConfigurationApplication.class)
public class PersonAPILiveTest {
private static String createPersonUrl;
private static String updatePersonUrl;
private static RestTemplate restTemplate;
private static HttpHeaders headers;
private final ObjectMapper objectMapper = new ObjectMapper();
private static JSONObject personJsonObject;
@BeforeClass
public static void runBeforeAllTestMethods() throws JSONException {
createPersonUrl = "http://localhost:8082/spring-rest/createPerson";
updatePersonUrl = "http://localhost:8082/spring-rest/updatePerson";
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
personJsonObject = new JSONObject();
personJsonObject.put("id", 1);
personJsonObject.put("name", "John");
}
@Test
public void givenDataIsJson_whenDataIsPostedByPostForObject_thenResponseBodyIsNotNull() throws IOException {
HttpEntity<String> request = new HttpEntity<String>(personJsonObject.toString(), headers);
String personResultAsJsonStr = restTemplate.postForObject(createPersonUrl, request, String.class);
JsonNode root = objectMapper.readTree(personResultAsJsonStr);
Person person = restTemplate.postForObject(createPersonUrl, request, Person.class);
assertNotNull(personResultAsJsonStr);
assertNotNull(root);
assertNotNull(root.path("name")
.asText());
assertNotNull(person);
assertNotNull(person.getName());
}
@Test
public void givenDataIsJson_whenDataIsPostedByPostForEntity_thenResponseBodyIsNotNull() throws IOException {
HttpEntity<String> request = new HttpEntity<String>(personJsonObject.toString(), headers);
ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(createPersonUrl, request, String.class);
JsonNode root = objectMapper.readTree(responseEntityStr.getBody());
ResponseEntity<Person> responseEntityPerson = restTemplate.postForEntity(createPersonUrl, request, Person.class);
assertNotNull(responseEntityStr.getBody());
assertNotNull(root.path("name")
.asText());
assertNotNull(responseEntityPerson.getBody());
assertNotNull(responseEntityPerson.getBody()
.getName());
}
@Test
public void givenDataIsJson_whenDataIsPostedByPostForLocation_thenResponseBodyIsTheLocationHeader() throws JsonProcessingException {
HttpEntity<String> request = new HttpEntity<String>(personJsonObject.toString(), headers);
URI locationHeader = restTemplate.postForLocation(updatePersonUrl, request);
assertNotNull(locationHeader);
}
}