Merge pull request #5462 from puneetd30/pd-Bael-2248
[BAEL-2248] Spring Rest Template - Accept form data
This commit is contained in:
commit
112e0db61c
|
@ -5,13 +5,9 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
|||
import org.baeldung.web.dto.Foo;
|
||||
import org.baeldung.web.dto.FooProtos;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
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.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
public class FooController {
|
||||
|
@ -47,7 +43,7 @@ public class FooController {
|
|||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/foos/new")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@ResponseBody
|
||||
public Foo createFoo(@RequestBody final Foo foo) {
|
||||
return foo;
|
||||
|
@ -60,4 +56,11 @@ public class FooController {
|
|||
return id;
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/foos/form")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@ResponseBody
|
||||
public String submitFoo(@RequestParam("id") String id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,8 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RequestCallback;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
@ -213,7 +215,23 @@ public class RestTemplateBasicLiveTest {
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
@Test
|
||||
public void givenFooService_whenFormSubmit_thenResourceIsCreated() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
|
||||
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
|
||||
map.add("id", "1");
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity( fooResourceUrl+"/form", request , String.class);
|
||||
|
||||
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||
final String fooResponse = response.getBody();
|
||||
assertThat(fooResponse, notNullValue());
|
||||
assertThat(fooResponse, is("1"));
|
||||
}
|
||||
|
||||
private HttpHeaders prepareBasicAuthHeaders() {
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
|
|
Loading…
Reference in New Issue