【发布时间】:2018-08-21 21:28:34
【问题描述】:
我正在开发一项服务,该服务接收来自 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 错误。
【问题讨论】:
-
为什么要包装模型而不是简单地返回
body作为方法结果?查看 Spring 入门指南以获取有关 REST 控制器的示例——看起来您正在尝试手动处理 Spring 自动执行的许多部分(如内容类型和 HTTP 状态)。 (此外,如果您遵循 Java 约定,例如将类命名为GlobalRequest,则阅读代码会容易得多。) -
我正在结束模型,因为我必须给第一个 API 的响应与正文不同。
标签: java json spring spring-boot