我正在客户端使用restTemplate.exchange或restTemplate.postForObject方法,在其中我通过标头和身份验证方法传递请求。
在与其他系统一起的服务器端,它将引发错误
ERROR com.....ExceptionHandler - Exception occurred while processing the request! javax.ws.rs.BadRequestException: HTTP 400 Bad Request
因此抛出错误
@Service("Service")
public class Service {
private static final Logger logger = LogManager.getLogger(Service.class);
@Autowired
private RestTemplate restTemplate;
@SuppressWarnings("rawtypes")
public String postRequest(String loanId, Request requestXml, Map<String, String> headersInfo) {
HttpHeaders headers = new HttpHeaders();
// ResponseEntity<GenericResponse<Response>> response = null;
Response response = null;
// String response = null;
List<String> riskReductionIdList = null;
String riskReductionId = null;
String responseString = null;
headers.setContentType(MediaType.APPLICATION_XML);
headers.set("-External-System-ID", headersInfo.get("ExternalSystemId"));
headers.set("-External-User-ID", headersInfo.get("ExternalUserId"));
String plainCredentials=headersInfo.get("User") + ":" + headersInfo.get("Password");
String base64Credentials = new String(Base64.encodeBase64(plainCredentials.getBytes()));
byte[] encodedAuth = Base64.encodeBase64(
plainCredentials.getBytes(Charset.forName("US-ASCII")) );
headers.add("Authorization", "Basic " + base64Credentials);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
StringWriter request = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Request.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(requestXml, request);
logger.error(" request : " + request.toString());
} catch (JAXBException e1) {
logger.error("Exception happened in coverting the request for logging",e1);
}
HttpEntity<?> httpEntity = new HttpEntity<Object>(requestXml, headers);
logger.error(" Request Body: " + httpEntity.getBody());
logger.error(" Request headers: " + httpEntity.getHeaders());
try{
// response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<GenericResponse<Response>>() {} );
response = restTemplate.postForObject(url, httpEntity, Response.class);
}catch(HttpClientErrorException ex){
logger.error(" Error while sending data to for loan id : " + loanId + "\n Error : "+ ex.getMessage());
logger.error(" responseBody : " + ex.getResponseBodyAsString() + "\n statusText : "+ ex.getStatusText() + "\n statusCode : "+ ex.getStatusCode());
}catch(HttpServerErrorException ex){
logger.error(" Error while sending data to for loan id : " + loanId + "\n Error : "+ ex.getMessage());
logger.error(" responseBody : " + ex.getResponseBodyAsString() + "\n statusText : "+ ex.getStatusText() + "\n statusCode : "+ ex.getStatusCode());
}catch(Exception ex){
logger.error(" Error while sending data to for loan id : " + loanId + "\n Error : "+ ex.getMessage());
}
responseString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> <response> <code>0</code> <status>SUCCESS</status> <messages/> <payload> "
+ " <riskReductionIds> <riskReductionId>10000</riskReductionId> </riskReductionIds> </payload> </response>";
return riskReductionId;
}
}
这是怎么回事? 我尝试过发送仅强制性参数的最小参数,尝试了exchange()和postForObject()方法。
从下面这个httpEntity生成的请求XML在邮递员中工作正常
这是我的服务等级-
=AVERAGE({1}:{2})
答案 0 :(得分:0)
rest我使用的模板有代码来转换JSOn对象。 我创建了一个restTemplate有效。