Skip to content

Commit 23c7fb4

Browse files
committed
[BAEL-2248] Add Controller Method to accept form data
Add Test method in RestTemplateBasicLiveTest
1 parent 3d51a0f commit 23c7fb4

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎spring-rest-simple/src/main/java/org/baeldung/web/controller/FooController.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import org.baeldung.web.dto.Foo;
66
import org.baeldung.web.dto.FooProtos;
77
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.MediaType;
89
import org.springframework.stereotype.Controller;
9-
import org.springframework.web.bind.annotation.PathVariable;
10-
import org.springframework.web.bind.annotation.RequestBody;
11-
import org.springframework.web.bind.annotation.RequestMapping;
12-
import org.springframework.web.bind.annotation.RequestMethod;
13-
import org.springframework.web.bind.annotation.ResponseBody;
14-
import org.springframework.web.bind.annotation.ResponseStatus;
10+
import org.springframework.web.bind.annotation.*;
1511

1612
@Controller
1713
public class FooController {
@@ -47,7 +43,7 @@ public FooProtos.Foo findProtoById(@PathVariable final long id) {
4743
}
4844

4945
@RequestMapping(method = RequestMethod.POST, value = "/foos/new")
50-
@ResponseStatus(HttpStatus.OK)
46+
@ResponseStatus(HttpStatus.CREATED)
5147
@ResponseBody
5248
public Foo createFoo(@RequestBody final Foo foo) {
5349
return foo;
@@ -60,4 +56,11 @@ public long deleteFoo(@PathVariable final long id) {
6056
return id;
6157
}
6258

59+
@RequestMapping(method = RequestMethod.POST, value = "/foos/form")
60+
@ResponseStatus(HttpStatus.CREATED)
61+
@ResponseBody
62+
public String submitFoo(@RequestParam("id") String id) {
63+
return id;
64+
}
65+
6366
}

‎spring-resttemplate/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.springframework.http.client.ClientHttpRequestFactory;
2727
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
2828
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
29+
import org.springframework.util.LinkedMultiValueMap;
30+
import org.springframework.util.MultiValueMap;
2931
import org.springframework.web.client.HttpClientErrorException;
3032
import org.springframework.web.client.RequestCallback;
3133
import org.springframework.web.client.RestTemplate;
@@ -213,7 +215,23 @@ public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
213215
}
214216
}
215217

216-
//
218+
@Test
219+
public void givenFooService_whenFormSubmit_thenResourceIsCreated() {
220+
HttpHeaders headers = new HttpHeaders();
221+
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
222+
223+
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
224+
map.add("id", "1");
225+
226+
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
227+
228+
ResponseEntity<String> response = restTemplate.postForEntity( fooResourceUrl+"/form", request , String.class);
229+
230+
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
231+
final String fooResponse = response.getBody();
232+
assertThat(fooResponse, notNullValue());
233+
assertThat(fooResponse, is("1"));
234+
}
217235

218236
private HttpHeaders prepareBasicAuthHeaders() {
219237
final HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)