![](/img/trans.png)
[英]Could not find acceptable representation - 406 Not Acceptable error
[英]How can i solve the 406 error, “Could not find acceptable representation”?
我正在開發一種服務,該服務接收來自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對象,但由於沒有意義,所以在此不包括它們。 因為我沒有收到406錯誤,所以我需要知道如何響應第一個API。
class Error { private String error; private String description; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.