可能重複:
HTTP GET with request body春天RestTemplate - HTTP GET請求與身體
我在這裏讀幾討論不提倡通過HTTP GET發送內容。可以通過客戶端(網絡瀏覽器)發送的數據大小有限制。處理GET數據也取決於服務器。請參考下面的資源。
但是,我被要求測試使用RestTemplate通過HTTP GET發送內容的可能性。我在春季論壇上討論了一些討論,但他們沒有回答。 (請注意通過http Post發送數據正常工作)。討論here建議使用POST代替。
開發ENV - JBoss應用服務器5.1,春季3.1.3
客戶
@Test
public void testGetWithBody()
{
// acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.TEXT_PLAIN);
// header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
// body
String body = "hello world";
HttpEntity<String> entity = new HttpEntity<String>(body, headers);
Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("id", "testFile");
// Send the request as GET
ResponseEntity<String> result = restTemplate.exchange(
"http://localhost:8080/WebApp/test/{id}/body",
HttpMethod.GET, entity, String.class, uriVariables);
Assert.assertNotNull(result.getBody());
}
服務器 @Controller
@RequestMapping(value = "/{id}/body", method = RequestMethod.GET)
public @ResponseBody
String testGetWithBody(@PathVariable String id,
@RequestBody String bodyContent)
{
return id + bodyContent;
}
問題 - 執行該測試案件返回50 0內部服務器錯誤。在調試時,我發現控制器沒有被擊中。
是否正確理解RestTemplate提供了發送數據請求體的方式,但出現錯誤的原因是服務器無法處理請求的身體嗎?
如果通過HTTP Get發送的請求體不是常規的,爲什麼RestTemplate會提供允許發送它的API?這是否意味着很少有服務器能夠通過GET處理請求主體?
資源 - 對在春季論壇
Resourc使用RestTemplate通過HTTP GET發送體的討論ES - 通過HTTP發送體一般討論GET
is-this-statement-correct-http-get-method-always-has-no-message-body
get-or-post-when-reading-request-body
as不允許在標記爲重複的問題中發帖回答我會在這裏導致花費大量時間來弄清楚如何去做 您可以使RestTemplate通過使用Apache的http客戶端與一些調整: – LifeIsGoodMF
class HttpGetWithBody extends HttpEntityEnclosingRequestBase {HttpGetWithBody(final URI uri){ setURI(uri); } @Override public String getMethod(){ return HttpGet.METHOD_NAME; } } – LifeIsGoodMF
HttpComponentsClientHttpRequestFactory requestFactory =新HttpComponentsClientHttpRequestFactory(){ @覆蓋 保護HttpUriRequest createHttpUriRequest(列舉HTTPMethod列舉HTTPMethod,URI URI){ 如果(列舉HTTPMethod == HttpMethod.GET){ 返回新HttpGetWithBody(URI); } return super.createHttpUriRequest(httpMethod,uri); } }; – LifeIsGoodMF