我在RestTemplate中使用GET方法的交换。但是在交换方法中传递requestEntity时,我收到了400错误的请求。 下面是我正在使用的代码。
产生网址:
@GetMapping("/getValues")
public ResponseEntity<ValueResponse> getValueCall(@RequestBody AccountDTO accountDTO) {
Map<Integer, ValueEntity> crmMap = new HashMap<Integer, ValueEntity>();
int accountNumber = accountDTO.getAccountNumber();
valueMap.put(1, new ValueEntity("A", "B", "Tier 1", "DU", "Indian"));
valueMap.put(2, new CRMEntity("C", "D", "Tier 2", "DU", "American"));
valueMap.put(3, new CRMEntity("E", "F", "Tier 3", "DU", "English"));
valueMap.put(4, new CRMEntity("G", "H", "Tier 1", "DU", "Australian"));
ValueResponse valueResponse = new ValueResponse();
for (Map.Entry<Integer, ValueEntity> entry : valueMap.entrySet()) {
if (entry.getKey().equals(accountNumber)) {
valueResponse.setAccountNumber(entry.getKey());
valueResponse.setFirstName(entry.getValue().getFirstName());
valueResponse.setLastName(entry.getValue().getLastName());
valueResponse.setCustomerTier(entry.getValue().getCustomerTier());
valueResponse.setCustomerType(entry.getValue().getCustomerType());
valueResponse.setNationality(entry.getValue().getNationality());
return new ResponseEntity<ValueResponse>(valueResponse, HttpStatus.OK);
}
}
return null;
}
使用网址:
public EnrollResponse enrollingUser(ValueDTO valueDTO) {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String,
String>();
headers.add("Content-Type", "application/json");
HttpEntity<ValueDTO> requestEntity = new HttpEntity<ValueDTO>(valueDTO,
headers);
ResponseEntity<ValueResponse> enrollResponseEntity =
restTemplate.exchange("http://localhost:8080/getValues",
HttpMethod.GET, requestEntity, ValueResponse.class);
return enrollResponseEntity.getBody();
}
实体和DTO:
public class ValueDTO {
private String firstName;
private String lastName;
private String dateOfBirth;
private String gender;
private String nationality;
private String email;
private String channelId;
private int accountNumber;
private String languageKnown;
//constructor
//getters and setters
}
public class ValueResponse {
private long accountNumber;
private String firstName;
private String lastName;
private String customerTier;
private String customerType;
private String nationality;
//constructor
//getters and setters
}
public class ValueEntity {
private String firstName;
private String lastName;
private String customerTier;
private String customerType;
private String nationality;
//constructor
//getters and setters
}
我尝试使用Header作为Content-Type:application / json和Body从邮递员访问生产者URL: { “ firstName”:“ A”, “ lastName”:“ B”, “ dateOfBirth”:“ 12/12/12”, “性别女”, “国籍”:“印度人”, “电子邮件”:“ abc@gmail.com”, “ channelId”:“ Values App”, “ accountNumber”:1 “ languageKnown”:“英语” } 作为valueDto对象。
控制台:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 null
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
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:778)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)
at com.smilesmember.domain.AccountDomain.enrollingUser(AccountDomain.java:184)
at com.smilesmember.controller.SmilesMemberController.enrollWS(SmilesMemberController.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
答案 0 :(得分:0)
如果您接受了您提到的ValueDTO,请尝试以下代码
ResponseEntity<ValueResponse> enrollResponseEntity =
restTemplate.exchange("http://localhost:8080/getValues",
HttpMethod.GET, requestEntity, new ParameterizedTypeReference<ValueResponse>() {});