java - 如何解决 406 错误“找不到可接受的表示”?
问题描述
我正在开发一种服务,它接收来自 API 的请求,并使用标头向其他 API 发出另一个请求。使用第二个 API 的数据,它完成了映射,然后我们用这些数据进行响应。在开发过程中我发现了这个错误,我无法解决它。我无法用正确的 JSON 响应 mi 第一个 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 错误。
解决方案
- 您的第二个 API 将直接返回 HTTP 错误 406 错误,第一个 api 需要处理它,因为它是第一个 api 的客户端。
- 如果您想设计一个 json,您可以创建一个 Error 对象并使用代码和描述在第一个和第二个 api 之间进行通信,如下所示
class Error {
private String error;
private String description;
}
推荐阅读
- c++ - 图像处理中的侵蚀
- python - 曲线拟合时如何限制 R 中的参数?
- python - 无法通过 XPATH 选择元素
- c# - 警告 NETSDK1080:针对 .NET Core 3.0 或更高版本时,不需要对 Microsoft.AspNetCore.App 的 PackageReference
- regex - Oracle SQL:组合多个 regexp_like 语句和正确的正则表达式语法
- php - 多表选择
- reactjs - 按索引或 id 切换本机反应
- javascript - JavaScript 倒计时功能
- bash - WSL - CMD 批处理脚本中的空格会破坏 bash 命令
- r - 代表错误(“”,空格1):无效的“次”参数