首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决406错误“找不到可接受的表示”?

如何解决406错误“找不到可接受的表示”?
EN

Stack Overflow用户
提问于 2018-08-22 05:28:35
回答 1查看 300关注 0票数 0

我正在开发一个服务,它接收来自API的请求,并使用头部向其他API发出另一个请求。使用第二个API的数据,它进行了映射,然后我们使用这些数据进行响应。在开发过程中,我发现了这个错误,我无法解决它。我不能用正确的JSON响应mi first API。

我的代码很简单,尽管杂乱无章。我有一个控制器和一个模型文件

控制器

代码语言:javascript
复制
@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);

}

另一边是模型

模型

代码语言:javascript
复制
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错误。

EN

回答 1

Stack Overflow用户

发布于 2018-08-22 06:12:12

你的第二个应用程序接口将直接抛回 error 406错误,第一个应用程序接口需要处理它,因为它是第一个应用程序接口的客户端。

  1. 如果你想设计一个json,你可以创建一个错误对象,并使用代码和描述在第一个和第二个api之间进行通信,比如so

class Error { private String error; private String description; }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51956936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档