解决 “no suitable HttpMessageConverter found” 错误

huangapple go评论114阅读模式
英文:

Solving "no suitable HttpMessageConverter found" error

问题

以下是您提供的代码的翻译部分:

  1. 我有一个简单的应用程序试图消费Rest服务
  2. @SpringBootApplication
  3. public class ConsumingRestApplication
  4. {
  5. private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class);
  6. public static void main(String[] args)
  7. {
  8. SpringApplication.run(ConsumingRestApplication.class, args);
  9. }
  10. @Bean
  11. public RestTemplate restTemplate(RestTemplateBuilder builder)
  12. {
  13. return builder.build();
  14. }
  15. @Bean
  16. public CommandLineRunner run(RestTemplate restTemplate) throws Exception
  17. {
  18. return args -> {
  19. try
  20. {
  21. restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
  22. } catch (RestClientException e)
  23. {
  24. e.printStackTrace();
  25. }
  26. };
  27. }
  28. }
  29. @JsonIgnoreProperties(ignoreUnknown = true)
  30. public class Quote {
  31. private String type;
  32. private Value value;
  33. public Quote() {
  34. }
  35. public String getType() {
  36. return type;
  37. }
  38. public void setType(String type) {
  39. this.type = type;
  40. }
  41. public Value getValue() {
  42. return value;
  43. }
  44. public void setValue(Value value) {
  45. this.value = value;
  46. }
  47. @Override
  48. public String toString() {
  49. return "Quote{" +
  50. "type='" + type + '\'' +
  51. ", value=" + value +
  52. '}';
  53. }
  54. }
  55. 遇到的错误
  56. org.springframework.web.client.UnknownContentTypeException: 无法提取响应找不到适合的HttpMessageConverter用于响应类型 [class com.example.consumingrest.Quote] 和内容类型 [application/json;charset=UTF-8]
  57. 完整的异常跟踪
  58. 2020-10-19 12:39:04.984 INFO 8328 --- [ restartedMain] c.e.c.ConsumingRestApplication : 正在启动 ConsumingRestApplication进程ID 8328位于 C:\gdrive\java_test\consumingrest\build\classes\java\main g C:\gdrive\java_test\consumingrest 中启动
  59. 2020-10-19 12:39:04.987 INFO 8328 --- [ restartedMain] c.e.c.ConsumingRestApplication : 未设置活动配置文件回退到默认配置文件default
  60. 2020-10-19 12:39:05.070 INFO 8328 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools 属性默认值处于活动状态spring.devtools.add-properties设置为false以禁用
  61. 2020-10-19 12:39:05.070 INFO 8328 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : 要获取更多与 Web 相关的日志记录请考虑将logging.level.web属性设置为DEBUG
  62. 2020-10-19 12:39:06.843 INFO 8328 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : 使用端口号初始化了 Tomcat8080http
  63. ...以下省略
  64. org.springframework.web.client.UnknownContentTypeException: 无法提取响应找不到适合的HttpMessageConverter用于响应类型 [class com.example.consumingrest.Quote] 和内容类型 [application/json;charset=UTF-8]
  65. at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
  66. at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741)
  67. at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
  68. at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:315)
  69. at com.example.consumingrest.ConsumingRestApplication.lambda$run$0(ConsumingRestApplication.java:37)
  70. ...以下省略

解决此问题的起始点是什么?我应该采取什么逻辑来找到问题?

英文:

I have simple application that is trying consume Rest service:

  1. @SpringBootApplication
  2. public class ConsumingRestApplication
  3. {
  4. private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class);
  5. public static void main(String[] args)
  6. {
  7. SpringApplication.run(ConsumingRestApplication.class, args);
  8. }
  9. @Bean
  10. public RestTemplate restTemplate(RestTemplateBuilder builder)
  11. {
  12. return builder.build();
  13. }
  14. @Bean
  15. public CommandLineRunner run(RestTemplate restTemplate) throws Exception
  16. {
  17. return args -> {
  18. try
  19. {
  20. restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
  21. } catch (RestClientException e)
  22. {
  23. e.printStackTrace();
  24. }
  25. };
  26. }
  27. }

Quote class:

  1. @JsonIgnoreProperties(ignoreUnknown = true)
  2. public class Quote {
  3. private String type;
  4. private Value value;
  5. public Quote() {
  6. }
  7. public String getType() {
  8. return type;
  9. }
  10. public void setType(String type) {
  11. this.type = type;
  12. }
  13. public Value getValue() {
  14. return value;
  15. }
  16. public void setValue(Value value) {
  17. this.value = value;
  18. }
  19. @Override
  20. public String toString() {
  21. return "Quote{" +
  22. "type='" + type + '\'' +
  23. ", value=" + value +
  24. '}';
  25. }
  26. }

Got error :

  1. org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.example.consumingrest.Quote] and content type [application/json;charset=UTF-8]

Whole exception trace:

  1. 2020-10-19 12:39:04.984 INFO 8328 --- [ restartedMain] c.e.c.ConsumingRestApplication : Starting ConsumingRestApplication on GM with PID 8328 (C:\gdrive\java_test\consumingrest\build\classes\java\main started by g in C:\gdrive\java_test\consumingrest)
  2. 2020-10-19 12:39:04.987 INFO 8328 --- [ restartedMain] c.e.c.ConsumingRestApplication : No active profile set, falling back to default profiles: default
  3. 2020-10-19 12:39:05.070 INFO 8328 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
  4. 2020-10-19 12:39:05.070 INFO 8328 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
  5. 2020-10-19 12:39:06.843 INFO 8328 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
  6. 2020-10-19 12:39:06.856 INFO 8328 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  7. 2020-10-19 12:39:06.857 INFO 8328 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
  8. 2020-10-19 12:39:06.957 INFO 8328 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  9. 2020-10-19 12:39:06.957 INFO 8328 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1887 ms
  10. 2020-10-19 12:39:07.204 INFO 8328 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  11. 2020-10-19 12:39:07.395 INFO 8328 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
  12. 2020-10-19 12:39:07.603 INFO 8328 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
  13. 2020-10-19 12:39:07.617 INFO 8328 --- [ restartedMain] c.e.c.ConsumingRestApplication : Started ConsumingRestApplication in 3.076 seconds (JVM running for 3.545)
  14. 2020-10-19 12:39:07.786 WARN 8328 --- [ restartedMain] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.consumingrest.Quote]]: com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class org.springframework.beans.factory.annotation.Value (of type annotation) as a Bean
  15. 2020-10-19 12:39:07.786 WARN 8328 --- [ restartedMain] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.consumingrest.Quote]]: com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class org.springframework.beans.factory.annotation.Value (of type annotation) as a Bean
  16. 2020-10-19 12:39:08.541 WARN 8328 --- [ restartedMain] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.consumingrest.Quote]]: com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class org.springframework.beans.factory.annotation.Value (of type annotation) as a Bean
  17. 2020-10-19 12:39:08.542 WARN 8328 --- [ restartedMain] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.consumingrest.Quote]]: com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class org.springframework.beans.factory.annotation.Value (of type annotation) as a Bean
  18. 2020-10-19 12:39:08.544 WARN 8328 --- [ restartedMain] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.consumingrest.Quote]]: com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class org.springframework.beans.factory.annotation.Value (of type annotation) as a Bean
  19. 2020-10-19 12:39:08.545 WARN 8328 --- [ restartedMain] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.consumingrest.Quote]]: com.fasterxml.jackson.databind.JsonMappingException: Cannot deserialize Class org.springframework.beans.factory.annotation.Value (of type annotation) as a Bean
  20. org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.example.consumingrest.Quote] and content type [application/json;charset=UTF-8]
  21. at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
  22. at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741)
  23. at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
  24. at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:315)
  25. at com.example.consumingrest.ConsumingRestApplication.lambda$run$0(ConsumingRestApplication.java:37)
  26. at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795)
  27. at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779)
  28. at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
  29. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
  30. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
  31. at com.example.consumingrest.ConsumingRestApplication.main(ConsumingRestApplication.java:21)
  32. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  33. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  34. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  35. at java.base/java.lang.reflect.Method.invoke(Method.java:567)
  36. at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

What is starting point o solving this problem? What logic I should go to find problem?

答案1

得分: 1

我认为您可能遗漏了 Value 对象,或者可能导入错误。除此之外,如果字段是私有的,在直接在响应中返回该对象时,您需要为这些字段添加 getter 方法。以下是我是如何做的:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.client.RestTemplateBuilder;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. @SpringBootApplication
  10. public class DemoApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(DemoApplication.class, args);
  13. }
  14. @Bean
  15. public RestTemplate restTemplate(RestTemplateBuilder builder) {
  16. return builder.build();
  17. }
  18. @RestController
  19. public static class Test {
  20. @Autowired
  21. private RestTemplate restTemplate;
  22. @GetMapping
  23. public Quote test() {
  24. return restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
  25. }
  26. }
  27. public static class Quote {
  28. private String type;
  29. private Value value;
  30. public String getType() {
  31. return type;
  32. }
  33. public Value getValue() {
  34. return value;
  35. }
  36. }
  37. public static class Value {
  38. private Long id;
  39. private String quote;
  40. public Long getId() {
  41. return id;
  42. }
  43. public String getQuote() {
  44. return quote;
  45. }
  46. }
  47. }
英文:

I believe you are missing Value object or must be a wrong import. Along with that you need to add getter methods for the field if they are private, when you are returning that object directly in the response, here is how i have done it:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.client.RestTemplateBuilder;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. @SpringBootApplication
  10. public class DemoApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(DemoApplication.class, args);
  13. }
  14. @Bean
  15. public RestTemplate restTemplate(RestTemplateBuilder builder) {
  16. return builder.build();
  17. }
  18. @RestController
  19. public static class Test {
  20. @Autowired
  21. private RestTemplate restTemplate;
  22. @GetMapping
  23. public Quote test() {
  24. return restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
  25. }
  26. }
  27. public static class Quote {
  28. private String type;
  29. private Value value;
  30. public String getType() {
  31. return type;
  32. }
  33. public Value getValue() {
  34. return value;
  35. }
  36. }
  37. public static class Value {
  38. private Long id;
  39. private String quote;
  40. public Long getId() {
  41. return id;
  42. }
  43. public String getQuote() {
  44. return quote;
  45. }
  46. }
  47. }

huangapple
  • 本文由 发表于 2020年10月19日 17:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64425017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定