简体   繁体   中英

How to pass object containing list of other custom objects to call REST service using HttpPost

I am trying to call REST service by passing object as parameter which contains list of other custom objects. I am getting "Error 405: Request method POST not supported" error. Client side code-

CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(SSLConnectionSocketFactory.getSystemSocketFactory()).build();

        httpPost = new HttpPost("http://api100.abc.xyz.com:9080/abcd/api/sscws/v1/saveContributions");
        httpPost.addHeader(WebAppConstants.CONTENT_TYPE, WebAppConstants.APPLICATION_JSON);
        httpPost.addHeader(WebAppConstants.ACCEPT, WebAppConstants.APPLICATION_JSON);
        httpPost.addHeader(WebAppConstants.X_USERNAME, userContext.getUserID());
        httpPost.addHeader(WebAppConstants.X_ENTERPRISE_ID, "123456");
        httpPost.addHeader(WebAppConstants.X_UNIQUE_ID, "A548742ATG");  //to do

        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String requestParamJson = ow.writeValueAsString(contribRequestParamDto);

        httpPost.setEntity(new StringEntity(contribRequestParamDto, WebAppConstants.UTF_8));

In above code contribRequestParamDto is a object of ContributionsRequestParamDto class which will look this-

public class ContributionsRequestParamDto {

private String tokenID;
private String taxID;
private String affiliateID;
private long planID;
private String accountNumber;
private String bankAccountNumber;
private String transitId;
private BigDecimal eftAmt;
private Date ppeDate;
private String taxYear;
private Short planType;

private List<ParticipantsDeferralDto> participantsDeferrals;
private List<EmployersContributionDto> employersContributions;

}

REST endpoint will look like this-

 @Transactional
@RestController
@RequestMapping("/v1")
@Api(value="v1", description="")
public class SscRestController {
    @RequestMapping(value="/saveContributions", 
            method=RequestMethod.POST, produces={MediaType.APPLICATION_JSON_VALUE}, consumes={MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value="Returns the saved contributions object")
    public String saveContributions(@RequestBody ContributionsRequestParam contributionsParam) throws Exception {
        return "success";
    }
}

Json request body is-

{
      "tokenID" : "123456789",
      "taxID" : "123456",
      "affiliateID" : "123456789",
      "planID" : 123456,
      "ppeDate" : "2017-10-24",
      "taxYear" : "2017",
      "planType" : 1,
      "participantsDeferrals" : [ {
        "taxId" : "555555",
        "participantDeferralAmt" : 22.00
      } ],
      "employersContributions" : [ {
        "taxId" : "555555",
        "employerContributionAmt" : 22.00
      } ]
    }

This is not working as I am getting "Error 405: Request method POST not supported" error. It will work if remove list fields for 'participantsDeferrals' and 'employersContributions' from JSON request body by removing those list fields from 'ContributionsRequestParamDto' object. So I am sure that there is something wrong with list of custom objects and its corresponding JSON request body. Am I missing something here? Please help. Thanks!

I have changed the type of the Date. Now it is normal String and not SQL date. After changing its type to string in object, it is working fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM