我正在开发一种服务,该服务接收来自API的请求,并且标头向另一个API发出另一个请求。使用第二个API的数据,它完成了映射,然后我们用这些数据进行响应。 在开发过程中,我发现此错误,无法解决。我无法使用针对mi first API的正确JSON进行响应。
我的代码很简单,但是很混乱。我有一个控制器和一个模型文件
控制器
@RestController
public class controller {
//Request of global API
@RequestMapping(value="orches", produces = MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
public model globalrequest(
@RequestHeader(value="Authorization") String Auth,
@RequestHeader(value="X-Country") String Country,
@RequestHeader(value="X-Global-Id") String LocalClid) throws JsonProcessingException, IOException {
// Country testing
String localApiUrl="";
switch (Country) {
case "SPA":
localApiUrl = "https://myapilocal";
default:
//error
};
RestTemplate restTemplate = new RestTemplate();
//Request of Local API
//header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("X-IBM-Client-Id", LocalClid);
headers.set("Authorization", Auth);
HttpEntity<String> entity = new HttpEntity<String>(headers);
//Send the request as GET
ResponseEntity<String> response = restTemplate.exchange(localApiUrl, HttpMethod.GET, entity, String.class);
//System.out.println(response);
String body = response.getBody();
return new model(body);
}
另一边是模型
模型
public class model {
private static String myJson = null;
ProfileGlo profileGlo = new ProfileGlo();
public model(String body) throws JsonProcessingException {
//super();
ProfileLoc profileLoc = new Gson().fromJson(body, ProfileLoc.class);
mapping(profileGlo, profileLoc);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
myJson = objectMapper.writeValueAsString(profileGlo);
System.out.println(myJson);
System.out.println(profileGlo.customerBasicData.customerNameData.firstName);
}
public static void mapping(ProfileGlo profileGlo, ProfileLoc profileLoc) {
if ( profileLoc.personType.equals("F")) {
// Mapping of the type of person
profileGlo.customerType = "1";
// Mapping of the Name
profileGlo.customerBasicData.customerNameData.firstName = profileLoc.fullName.getName();
//Mapping of the lastNames
profileGlo.customerBasicData.customerNameData.middleName = profileLoc.fullName.getLastName();
//Mapping companyName
profileGlo.sMEBusinessCustomerBasicData.companyName = null;
//Mapping birthDate
profileGlo.customerBasicData.birthDate = profileLoc.birthDate;
}
else if ( profileLoc.personType.equals("J")) {
// Mapping of the type of person
profileGlo.customerType = "2";
// Mapping of the Name
profileGlo.sMEBusinessCustomerBasicData.companyName = profileLoc.fullName.getName();
//Mapping of the lastNames
profileGlo.customerBasicData.customerNameData.middleName = null;
//Mapping companyName
profileGlo.sMEBusinessCustomerBasicData.companyName = profileLoc.fullName.getCompanyName();
//Mapping birthDate
profileGlo.customerBasicData.birthDate = null;
}
else {
//error500
}
}
@ResponseBody
public String orches(HttpServletResponse response) {
response.addHeader("content-type", "application/json");
response.setStatus(200);
return myJson;
}
}
还定义了ProfileGlo和ProfileLoc对象,但由于没有意义,因此在此不包括它们。 我需要知道如何响应第一个API,因为我没有收到406错误。
答案 0 :(得分:0)
class Error {
private String error;
private String description;
}