代码之家  ›  专栏  ›  技术社区  ›  N Kan

使用Spring Boot的REST客户端

  •  0
  • N Kan  · 技术社区  · 6 年前

    我正在使用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>)
    

    应用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());
                }
            };
        }
    }
    

    引用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 回复  |  直到 6 年前
        1
  •  0
  •   ikos23    6 年前

    下面的行输入方法肯定有问题 run() :

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

    阵列。asList 签名如下:

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

    因此,您可以这样使用它(如果需要传递多个参数):

    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 stuff配合使用。