使用Spring Boot的REST客户端

时间:2018-05-16 13:15:24

标签: spring rest spring-boot client

我正在使用spring网站上的示例编写REST Web服务的客户端 https://spring.io/guides/gs/consuming-rest/

我的局限是我必须使用JDK1.6进行此开发。

客户端应用程序(用匿名方法调用替换Lambda)抛出错误,我无法找到修复程序。任何帮助将不胜感激。

错误信息是:

The method setSupportedMediaTypes(List<MediaType>) in the type AbstractHttpMessageConverter<Object> is not applicable for the arguments (List<Object>)

Application.java

package main.java.hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;;
//import com.fasterxml.jackson.core.*;

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String args[]) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    public CommandLineRunner run(final RestTemplate restTemplate) throws Exception {
        return new CommandLineRunner() {

            @Override
            public void run(String... args) throws Exception {

                List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();        

                //Add the Jackson Message converter
                MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

                // Note: here we are making this converter to process any kind of response, 
                // not only application/*json, which is the default behaviour
                converter.setSupportedMediaTypes(Arrays.asList({MediaType.ALL}));         
                messageConverters.add(converter); 

                Quote quote = restTemplate.getForObject(
                        "http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
                log.info(quote.toString());
            }
        };
    }
}

Quote.java

package main.java.hello;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

    private String type;
    private Value value;

    public Quote() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ", value=" + value +
                '}';
    }
}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>WebServiceClient</groupId>
  <artifactId>WebServiceClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.17.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.17.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.5.13.RELEASE</version>
    </dependency>    

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.5.13.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>



    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.5</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.5</version>
    </dependency>  

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
    </dependency>   

  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

谢谢!

1 个答案:

答案 0 :(得分:0)

方法run()中的以下行肯定存在问题:

converter.setSupportedMediaTypes(Arrays.asList({MediaType.ALL})); 

Arrays.asList 具有以下签名:

public static <T> List<T> asList(T... a) { ... }

所以你要么像这样使用它(如果你需要传递多个args):

converter.setSupportedMediaTypes(Arrays.asList(new MediaType[]{MediaType.ALL}));

converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));

请尝试这一点并检查它是否有任何区别。

<强>更新

对于JDK 1.6,您可能需要将Jackson降级到1.9.13。 我找到了一些东西:

<!-- core on 1 version -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<!-- databind in version 1 -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

但是我找不到jackson-annotations的任何内容,没有版本1的类似模块。也许你可以尝试没有它,看看它是否有效。

不确定它将如何与Spring版本的新版本一起使用。