获取text / json内容类型时,Spring API应用程序/ json无效

时间:2017-09-11 08:54:46

标签: json spring api

我正在查询PhraseApp API以获取翻译并在标题上设置内容类型为appplication / json:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

但API会将响应作为附件发送,内容类型为text/json; charset=UTF-8。 Spring引发了错误:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.String] and content type [text/json;charset=UTF-8]

我得到的回应如下:

{
  "some.key.a": "some translation for a",
  "some.key.b": "some translation for b",
  "some.key.c": "some translation for c"
...
}

我希望将响应分配给String变量并稍后解析它。 对这里出了什么问题有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:0)

根据JSON-specification,JSON的内容类型应始终为" application / json"。

一种可能的解决方法是编辑请求并设置正确的内容类型。

幸运的是,我来自PhraseApp团队,可以简单地解决这个问题;)

您现在应该能够将它与原始代码一起使用!

如果您需要进一步的帮助,请发送电子邮件至vincent@phraseapp.com

或发送电子邮件给我

答案 1 :(得分:0)

我必须使用以下步骤才能使其正常工作:

  1. 使用find /a /b /c /d -name '*.png' -exec cp /MMM.jpg {} \;

  2. 按如下方式创建自定义转换器:

    公共类PhraseTextJsonConverter扩展MappingJackson2HttpMessageConverter {     public PhraseTextJsonConverter(){         setSupportedMediaTypes(initMediaTypes());     }

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        int num;
        int dig[3];
        int i,j = 100;
        int result = 0;
        for(i = 0;i<3;i++){
            fflush(stdin);
            printf("Please enter the %d digit of your three digit number:",i+1);
            scanf("%d", &dig[i]);
        }
    
        for(i = 0;i<3;i++){
            result += pow(dig[i],3);
            num += dig[i] * j;
            j/=10;
            printf("%d * %d = %d\n",dig[i],j,num);
        }
    
        if (num == result)
        {
            printf("Your number is an Armstrong number!\n");
        }
        else
        {
            printf("Your number is not an Armstrong number!\n");
        }
    
        system("pause");
        return 0;
    }
    

    }

  3. 将转换器添加到模板中:

    /api/v2/projects/{projectId}/locales/{localeId}/download

    以后是自定义的:

    private List<MediaType> initMediaTypes() {
        return Arrays.asList(new MediaType("text", "json", DEFAULT_CHARSET));
    }
    
  4. 为了能够将所有翻译作为地图,我必须使用protected static RestTemplate createRestTemplateWithConverter() { final RestTemplate restTemplate = new RestTemplate(); final List<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<>(); httpMessageConverters.add(new MappingJackson2HttpMessageConverter()); httpMessageConverters.add(new ByteArrayHttpMessageConverter()); restTemplate.setMessageConverters(httpMessageConverters); return restTemplate; } ,如下所示:

    private void addPhraseTextJsonConverter() {
            restTemplate.getMessageConverters().add(new PhraseTextJsonConverter());
        }
    
    1. 不要忘记初始化请求参数以获取Map中的所有翻译:

      列出urlParameters = new ArrayList&lt;&gt;(); urlParameters.add(new BasicNameValuePair(&#34; file_format&#34;,&#34; simple_json&#34;));

    2. 那就是它。