java - 在 Spring Boot 中使用 REST API 时出错

标签 java spring rest spring-mvc spring-boot

我正在尝试使用 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);
        };
    }

}

NewOrder.java

public class NewOrder {
    String orderId;
}

build.gradle

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

最佳答案

 I have used restTemplate.exchange but you can modify to use postForEntity 
 or postForObject. Hope this helps.

 private HttpHeaders createHttpHeaders() {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return headers;
    }

@RequestMapping("/testClient")
public String testClient() {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = createHttpHeaders();
    HttpEntity<String> entity = new HttpEntity<String>("parameters",
            headers);
    ResponseEntity<String> response = restTemplate.exchange(url,
            HttpMethod.GET, entity, String.class);
    if (response.getStatusCode().equals(HttpStatus.OK)) {
        System.out.println("getbody -" + response.getBody());
    }
    return "Test Client";
}

关于java - 在 Spring Boot 中使用 REST API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012184/

相关文章:

java - 使用 Java 解析 Facebook signed_request 返回格式错误的 JSON

java - 对java属性文件位置感到困惑

java - 如何找到 Java 依赖项?

java - 如何使用 java spring 从选择输入选项中检索文本

java - 如何指定枚举对象数组的 RESTful 资源查询参数

java - 如何做一个随机且唯一的生成器?

java - jOOQ 和 Spring 事务管理

java - Spring 安全和 Thymeleaf 不起作用

rest - CKAN 通过 Postman 将文件上传到数据集(手动 HTTP 请求)

python - 如何将 django-rest-swagger 2.0 与现有 DRF 应用程序集成(初学者)