在Spring引导中使用REST API时发生java错误
我试图使用REST
API,它接受JSON
有效负载,并在响应中返回纯文本。但我在运行时遇到以下错误
SpotifyIntegrationApplication。java
@SpringBootApplication
public class SpotifyIntegrationApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(SpotifyIntegrationApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpotifyIntegrationApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) {
NewOrder newOrder = new NewOrder();
return args -> {
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:9999/order-sold", newOrder, String.class);
LOGGER.info("responseEntity: " + responseEntity);
};
}
}
新秩序。java
public class NewOrder {
String orderId;
}
构建。格拉德尔
buildscript {
ext {
springBootVersion = '1.5.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.synapse.integration'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
runtime('mysql:mysql-connector-java')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
错误:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at com.synapse.integration.spotify.SpotifyIntegrationApplication.main(SpotifyIntegrationApplication.java:20) [classes/:na]
Caused by: org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.synapse.integration.spotify.model.NewOrder]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:907) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:658) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:415) ~[spring-web-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at com.synapse.integration.spotify.SpotifyIntegrationApplication.lambda$run$0(SpotifyIntegrationApplication.java:32) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
... 6 common frames omitted
# 1 楼答案
嗯,当查看Spring的文档时,答案都有点混乱: https://spring.io/guides/gs/consuming-rest/
似乎您应该能够成功地使用响应,通过带注释的对象映射到JSON对象
然而,即使这样做:
我仍然得到上面的未能执行CommandLineRunner错误,将不得不调查它是否值得映射到HttpResponseObject。希望大家对我的评论有一些想法
# 2 楼答案
从上面的回答中,再加上一些额外的谷歌搜索,找到了解决方案
SpotifyIntegrationApplication。java
新秩序。java
虽然这不是原始问题的一部分,但也删除了Tomcat库依赖项,因为我只是在使用API,而不是构建API
构建。格拉德尔