我正在开发一个服务,它接收来自API的请求,并使用头部向其他API发出另一个请求。使用第二个API的数据,它进行了映射,然后我们使用这些数据进行响应。在开发过程中,我发现了这个错误,我无法解决它。我不能用正确的JSON响应mi first API。
我的代码很简单,尽管杂乱无章。我有一个控制器和一个模型文件
控制器
@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错误。
发布于 2018-08-22 06:12:12
你的第二个应用程序接口将直接抛回 error 406错误,第一个应用程序接口需要处理它,因为它是第一个应用程序接口的客户端。
。
class Error { private String error; private String description; }
https://stackoverflow.com/questions/51956936
复制相似问题