Использование службы REST - метод POST - PullRequest
0 голосов
/ 01 февраля 2020

Когда я использую SoapUI, это работает. Но мой клиентский код дает org.springframework.web.client.HttpClientErrorException $ BadRequest: 400

Я полагаю, что при вызове остальных шаблонов службы происходит сбой в классе обслуживания.

в организации. springframework.web.client. handleError (DefaultResponseErrorHandler. java: 112) в org.springframework.web.client.ResponseErrorHandler.handleError (ResponseErrorHandler. java: 63) в org.springframework.web.client.Reslate.Reslate .Rest 785) в org.springframework.web.client.RestTemplate.doExecute (RestTemplate. java: 743) в org.springframework.web.client.RestTemplate.execute (RestTemplate. java: 677) в org.springbrame.ra .client.RestTemplate.exchange (RestTemplate. java: 586) на com.spectrum.biller.service .BillerService.getBillersSoloIdentifiers (BillerService. java: 61) в com.spectrum.biller.controllers.BillerControllers.getSoloIdentifiers (BillerControllers. java: 33)


@SpringBootApplication
public class BillerApplication {

    private static final Logger log = LoggerFactory.getLogger(BillerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(BillerApplication.class, args);
    }
}

package com.spectrum.biller.controllers;

import org.springframework.web.bind.annotation.RestController;

import com.spectrum.biller.exception.BillerException;
import com.spectrum.biller.model.GetSoloIdentifiersRequest;
import com.spectrum.biller.model.GetSoloIdentifiersResponse;
import com.spectrum.biller.service.BillerService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
@RequestMapping("/biller")
@ComponentScan("com.spectrum.biller")
public class BillerControllers {

    private static final Logger log = LoggerFactory.getLogger(BillerControllers.class);

    @PostMapping(value = "/soloIdentifiers", produces= {"application/json"})
    public ResponseEntity<GetSoloIdentifiersResponse> getSoloIdentifiers(
            @RequestBody(required=false) GetSoloIdentifiersRequest getSoloIdentifiersRequest 
            ) {     
        log.info("Entering BillerControllers getSoloIdentifiers..." );
        try {
            BillerService billerService = new BillerService();
            return billerService.getBillersSoloIdentifiers(getSoloIdentifiersRequest);
        } catch ( Exception e) {
            throw new BillerException("Error in Biller Client " + e.getMessage() + e);
        }
    } 

}


@Service
public class BillerService {

    private static final Logger log = LoggerFactory.getLogger(BillerService.class);

    //@Value("${biller.service.url}")
    private String billerServiceUrl =  "https://spectrumcore.myCompany.com/spectrum-core/services/customer/ept/getSoloIdentifiersV1x0";         

    public ResponseEntity<GetSoloIdentifiersResponse> getBillersSoloIdentifiers(GetSoloIdentifiersRequest getSoloIdentifiersRequest) {
        log.info("Entering BillerService getSoloIdentifiers..." );
        ResponseEntity<GetSoloIdentifiersResponse> response = null;
        try {
             log.debug("systemID = " + getSoloIdentifiersRequest.getSystemID() );

             RestTemplate restTemplate = new RestTemplate();

             HttpHeaders headers = new HttpHeaders();
             headers.setContentType(MediaType.APPLICATION_JSON);
             headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

             HttpEntity<GetSoloIdentifiersRequest> requestEntity =
                     new HttpEntity<>(getSoloIdentifiersRequest, headers);
            log.debug("Request = " + getSoloIdentifiersRequest.toString()); 
            log.debug("requestEntity = " + requestEntity);

             response = 
                     restTemplate.exchange(billerServiceUrl, HttpMethod.POST, requestEntity,GetSoloIdentifiersResponse.class);

             log.debug("result =" + response.toString());
             return response; 

       } catch ( Exception e) {
           System.out.println(e.getMessage() + e);

       }
        return response; 
   }
}


**Here is my payload in postman**

{
    "systemID": "ProvSvcs",
    "divisionID" : "NTX.8260",
    "accountNumber" : "8260170400116033"
}



Сапуи использует следующий запрос и ответ.

     <GetSoloIdentifiersRequest> 
     <systemID>ProvSvcs</systemID> 
     <divisionID>${#TestSuite#DIVISION_ID}</divisionID> 
     <accountNumber>${#TestSuite#BILLING_ACCOUNT_NUMBER}</accountNumber>
     </GetSoloIdentifiersRequest>

<getSoloIdentifiersResponse>
   <uCAN>59868711478</uCAN>
   <divisionID>NTX.8260</divisionID>
   <accountNumber>8260170400116033</accountNumber>
   <locationNumber>79821513800008</locationNumber>
   <customerNumber>1104616236633</customerNumber>
   <soloAccountNumber>69095406</soloAccountNumber>
   <soloLocationNumber>235601644</soloLocationNumber>
   <soloPartyID>35936825</soloPartyID>
   <billingStationLevel1Code>8260</billingStationLevel1Code>
   <billingStationLevel2Code>1700</billingStationLevel2Code>
   <billingStationID>32456249</billingStationID>
   <sourceFTACode>0400</sourceFTACode>
</getSoloIdentifiersResponse>

Итак, моя POST-транзакция не работает. Я также пытался использовать библиотеку Джерси, она не работает.

Я получаю org.springframework.web.client.HttpClientErrorException $ BadRequest: 400

...