使用Spring REST模板POST POST JSON对象

时间:2022-07-19 20:37:41

I am posting JSON object using spring rest template. It works fine for less data, but posting more data throws a Request URI too long error.

我使用spring rest模板发布JSON对象。它适用于较少的数据,但发布更多数据会引发Request URI太长的错误。

       final String url = getServiceUrl() + "/rs/doc?param1=test";

        RestTemplate restTemp=getRestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);

        //set your entity to send
        HttpEntity<MyBean> request = new HttpEntity<MyBean>(myBean,headers);

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new MappingJacksonHttpMessageConverter());
        messageConverters.add(new FormHttpMessageConverter());
        restTemp.getMessageConverters().addAll(messageConverters);

        // send it!
        responseEntity = restTemp.exchange(url, HttpMethod.POST, request, String.class);

The request body should accept unlimited data in POST method. But that doesn't seem to work here. Can someone please guide.

请求正文应该在POST方法中接受无限数据。但这似乎不适用于此。有人可以指导。

1 个解决方案

#1


1  

Below is working fine for me. I have added security details in the header and the post parameters that I need to sent.

以下对我来说很好。我在头部和我需要发送的帖子参数中添加了安全性详细信息。

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set(ApplicationConstants.API_KEY, ApplicationConstants.TEST_API_KEY_VALUE);
headers.set(ApplicationConstants.AUTH_TOKEN, ApplicationConstants.TEST_API_TOKEN_VALUE);
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
postParameters.add("purpose", cust.getPaymentPurpose());
postParameters.add("buyer_name", cust.getCustomerName());
postParameters.add("email", cust.getCustomerEmailId());
postParameters.add("phone", cust.getCustomerMobNum());
postParameters.add("send_email", "False");
postParameters.add("send_sms", "False");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers);
ResponseEntity<String> result = restTemplate.exchange("YOUR URL", HttpMethod.POST, requestEntity, String.class);
OnlinePaymentModel paymentModel = gson.fromJson(result.getBody(), OnlinePaymentModel.class);

#1


1  

Below is working fine for me. I have added security details in the header and the post parameters that I need to sent.

以下对我来说很好。我在头部和我需要发送的帖子参数中添加了安全性详细信息。

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set(ApplicationConstants.API_KEY, ApplicationConstants.TEST_API_KEY_VALUE);
headers.set(ApplicationConstants.AUTH_TOKEN, ApplicationConstants.TEST_API_TOKEN_VALUE);
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
postParameters.add("purpose", cust.getPaymentPurpose());
postParameters.add("buyer_name", cust.getCustomerName());
postParameters.add("email", cust.getCustomerEmailId());
postParameters.add("phone", cust.getCustomerMobNum());
postParameters.add("send_email", "False");
postParameters.add("send_sms", "False");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers);
ResponseEntity<String> result = restTemplate.exchange("YOUR URL", HttpMethod.POST, requestEntity, String.class);
OnlinePaymentModel paymentModel = gson.fromJson(result.getBody(), OnlinePaymentModel.class);