- 追加された行はこの色です。
- 削除された行はこの色です。
-SpringでHttpアクセスをするなら使うべし
*実装 [#je5b447d]
**Entityにマッピング [#s7e49ec2]
200以外のレスポンスだったり、JSONなのにtext/htmlだったりするとNULLが帰ってくる。
**XMLをPOSTする [#ba9d3f32]
-StringにXMLをつめて、HTTPEntity作成時にStringを渡す
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
public class RestTemplateTest {
public static void main(String[] args) throws IOException {
String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><AvailReq><hotelid>123</hotelid></AvailReq>";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<String> request = new HttpEntity<String>(xmlString, headers);
final ResponseEntity<String> response = restTemplate.postForEntity("http://www.rutake.com/", request, String.class);
}
}
*例外 [#see495e8]
|繋がらない|org.springframework.web.client.ResourceAccessException|
|200以外|org.springframework.web.client.HttpClientErrorException: 400 Bad Request|
*テスト [#ce675ce7]
MockRestServiceServerを使うとレスポンスを偽装できる。