发送发布请求引发500内部错误

时间:2020-05-16 05:00:42

标签: java json spring-boot http-post

每次我尝试发送一个发布请求时,都会出现500错误。通过邮递员进行的发布请求也可以正常工作,因此服务器端没有问题。什么是错误?

控制器

package com.neostencil.services;

import com.neostencil.leadsquard.LeadCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class LeadSquardService {

  private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

  @Value("${leadsquard.accesskey}")
  private String access_key;

  @Value("${leadsquard.secretkey}")
  private String serect_key;

  /**
   * Create a New User in Lead Capture.
   * @param leadCapture
   * */
  public void createNewUserInLeadCapture(LeadCapture leadCapture) {

    MultiValueMap<String, String> mapdata = new LinkedMultiValueMap<>();
    mapdata.add("FirstName", leadCapture.getFirstName());
    mapdata.add("Phone", "");
    mapdata.add("EmailAddress", leadCapture.getEmailAddress());
    mapdata.add("mx_Courses", leadCapture.getMx_courses());


    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("hostUrl");
    builder.queryParam("accessKey" , access_key );
    builder.queryParam("secretKey", serect_key);


    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    //headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    try {
      HttpEntity<MultiValueMap<String,String>> requestEntity = new HttpEntity<>(mapdata, headers);


      RestTemplate restTemplate = new RestTemplate();
      ResponseEntity<String> response = restTemplate.exchange(builder.build().toUriString(), HttpMethod.POST, requestEntity, String.class);

      HttpStatus statusCode = response.getStatusCode();
      if (statusCode == HttpStatus.ACCEPTED) {

        System.out.println("ResponseEntity : " + response.getBody());
      }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

PrintStackTrace

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error
    at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:79)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:124)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578)
    at com.neostencil.services.LeadSquardService.createNewUserInLeadCapture(LeadSquardService.java:57)
    at ...

1 个答案:

答案 0 :(得分:0)

您可以尝试传递JSON String

HttpEntity<String> requestEntity = new HttpEntity<>(new Gson().toJson(mapdata), headers);

更好的选择是创建一个POJO来映射请求。

HttpEntity<User> requestEntity = new HttpEntity<User>(user, headers);