在Spring Boot中使用REST API时出错

Nital:

我正在尝试使用REST需要JSON有效负载并在响应中返回纯文本的API 但是我在运行时出现以下错误。

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
JaisAnkit:
 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";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Spring Boot Data JPA Rest API中使用PostgreSql时找不到表错误

在Spring Boot的REST API中使用Jersey而不是Spring MVC的优势是什么?

从另一个Rest API调用一个Spring Boot Rest API时出错

在AWS BeanStalk中使用Spring Boot REST API部署React App

如何在Android应用中使用Retrofit 2消费Spring-Boot REST API

在Spring Boot Rest API中使用多个联接公开自定义查询

在 Spring Boot 中使用 FeignClient 进行同步 Rest API 调用

在 Spring Boot 应用程序中使用 JAX-RS REST API 作为库

在Spring中使用表达语言时出错

启动Spring Boot时出错

使用 Spring Boot 创建 Elasticsearch API Client(8.0.1) 对象时出错

使用LDAP认证的Spring Boot REST API

使用Rest API 404的Spring Boot

使用 java 和 spring boot 的 REST api

在使用JHipster创建的Spring Boot应用程序中使用npm install时出错

在Spring Boot中使用令牌保护API

使用Spring Boot运行Spring Integration RSocket示例时出错

在spring-boot中使用kotlin暂停功能创建名称为'requestMappingHandlerMapping'的bean时出错

如何使用Spring Boot和Spring Security保护REST API?

在 tomcat 外部服务器上部署 spring boot restfull api 时出错

在基于Spring Boot的应用程序中使用REST API输入在运行时设置架构名称

Spring Boot异常,创建bean时出错

创建名为'requestMappingHandlerMapping'的Spring Boot时出错

凝视Spring Boot Kafka项目时出错

启动 Spring boot Eureka Client 时出错

使用基本 Spring Boot 身份验证时出错

使用spring boot和testng运行集成测试时出错

使用SpringToolSuite打开新的Spring Boot项目时出错

使用 xsd spring boot 验证 xml 时出错