Java 获取proxyBeanMethods()NosuchMethod异常

Java 获取proxyBeanMethods()NosuchMethod异常,java,web-services,Java,Web Services,我编写了一个web服务来连接一个具有自定义参数的URL import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collectio

我编写了一个web服务来连接一个具有自定义
参数的URL

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ServiceNowController {

    private static final Logger logger = LoggerFactory.getLogger(ServiceNowController.class);

    @CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
    @RequestMapping(method = RequestMethod.GET, value = "/incident", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> retriveAllIncidents(HttpServletRequest request) throws UnsupportedEncodingException {
        logger.info("ServiceNowController -> retrieveAllIncidents(): Invoked");
        RestTemplate restTemplate = new RestTemplate();

        ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
        messageConverters.add(converter);
        restTemplate.setMessageConverters(messageConverters);

        String mainUrl = "<url>";
        final String sysparm_query = "param1";
        final String sysparm_display_value = "param2";
        final String decoded_sysparam_query = URLDecoder.decode(sysparm_query, StandardCharsets.UTF_8.toString());

        System.out.println(decoded_sysparam_query);

        try {

            URIBuilder builder = new URIBuilder(mainUrl);
            builder.addParameter(sysparm_query, decoded_sysparam_query);
            builder.addParameter(sysparm_display_value, sysparm_display_value);
            String finalUrl = builder.toString();
            System.out.println(finalUrl);

            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "<authorization token>");
            headers.setAccept(Arrays.asList(MediaType.ALL));

            HttpEntity<String> entity =  new HttpEntity<String>(headers);
            String urlString = "< url with params >";
            String encodedUrl = URLEncoder.encode(urlString, StandardCharsets.UTF_8.toString());
            String decodedUrl = URLDecoder.decode(encodedUrl.toString(), StandardCharsets.UTF_8.toString());

            ResponseEntity<Object> results = restTemplate.exchange(decodedUrl, HttpMethod.GET, null, Object.class);
            return results;

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;

    }

}  
POM.XML

package com.dell.servicenowapis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ServicenowapisApplication {

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

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}  
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dell</groupId>
    <artifactId>servicenowapis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>servicenowapis</name>
    <description>Service Now API</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>  

4.0.0
org.springframework.boot
spring启动程序父级
2.2.1.发布
com.dell
ServiceNowapi
0.0.1-快照
ServiceNowapi
立即服务API
1.8
公用记录
公用记录
1.2
org.springframework.boot
弹簧靴起动器
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测验
org.apache.httpcomponents
httpclient
com.fasterxml.jackson.core
杰克逊数据绑定
通用编解码器
通用编解码器
org.springframework.boot
springbootmaven插件
当我尝试运行它时,我得到了这个堆栈跟踪

org.springframework.core.annotation.AnnotationConfigurationException: Attribute 'proxyBeanMethods' in annotation [org.springframework.boot.SpringBootConfiguration] is declared as an @AliasFor nonexistent attribute 'proxyBeanMethods' in annotation [org.springframework.context.annotation.Configuration].; nested exception is java.lang.NoSuchMethodException: org.springframework.context.annotation.Configuration.proxyBeanMethods()
    at org.springframework.core.annotation.AnnotationUtils$AliasDescriptor.<init>(AnnotationUtils.java:2089)
    at org.springframework.core.annotation.AnnotationUtils$AliasDescriptor.from(AnnotationUtils.java:2056)
    at org.springframework.core.annotation.AnnotationUtils.getAttributeAliasNames(AnnotationUtils.java:1726)
    at org.springframework.core.annotation.AnnotationUtils.isSynthesizable(AnnotationUtils.java:1685)
    at org.springframework.core.annotation.AnnotationUtils.synthesizeAnnotation(AnnotationUtils.java:1468)
    at org.springframework.core.annotation.AnnotationUtils.synthesizeAnnotationArray(AnnotationUtils.java:1572)
    at org.springframework.core.annotation.AnnotationUtils.getAnnotations(AnnotationUtils.java:231)
    at org.springframework.core.type.classreading.AnnotationAttributesReadingVisitor.visitEnd(AnnotationAttributesReadingVisitor.java:76)
    at org.springframework.asm.ClassReader.readAnnotationValues(ClassReader.java:2020)
    at org.springframework.asm.ClassReader.accept(ClassReader.java:676)
    at org.springframework.asm.ClassReader.accept(ClassReader.java:527)
    at org.springframework.ide.eclipse.core.java.classreading.JdtConnectedMetadataReader.<init>(JdtConnectedMetadataReader.java:45)
    at org.springframework.ide.eclipse.core.java.classreading.JdtMetadataReaderFactory.getMetadataReader(JdtMetadataReaderFactory.java:53)
    at org.springframework.ide.eclipse.core.java.classreading.CachingJdtMetadataReaderFactory.getMetadataReader(CachingJdtMetadataReaderFactory.java:38)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig.registerBean(BeansJavaConfig.java:368)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$2.call(BeansJavaConfig.java:229)
    at org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$2.call(BeansJavaConfig.java:1)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: org.springframework.context.annotation.Configuration.proxyBeanMethods()
    at java.lang.Class.getDeclaredMethod(Unknown Source)
    at org.springframework.core.annotation.AnnotationUtils$AliasDescriptor.<init>(AnnotationUtils.java:2082)
    ... 22 more  
org.springframework.core.annotation.AnnotationConfigurationException:annotation[org.springframework.boot.SpringBootConfiguration]中的属性“proxyBeanMethods”被声明为annotation[org.springframework.context.annotation.Configuration]中不存在的属性“proxyBeanMethods”的@alias。;嵌套异常是java.lang.NoSuchMethodException:org.springframework.context.annotation.Configuration.proxyBeanMethods()
位于org.springframework.core.annotation.AnnotationUtils$AliasDescriptor。(AnnotationUtils.java:2089)
位于org.springframework.core.annotation.AnnotationUtils$AliasDescriptor.from(AnnotationUtils.java:2056)
位于org.springframework.core.annotation.AnnotationUtils.getAttributeAliasNames(AnnotationUtils.java:1726)
位于org.springframework.core.annotation.AnnotationUtils.issynthesized(AnnotationUtils.java:1685)
位于org.springframework.core.annotation.AnnotationUtils.synthesizeAnnotation(AnnotationUtils.java:1468)
位于org.springframework.core.annotation.AnnotationUtils.synthesis AnnotationArray(AnnotationUtils.java:1572)
位于org.springframework.core.annotation.AnnotationUtils.getAnnotations(AnnotationUtils.java:231)
位于org.springframework.core.type.classreading.AnnotationAttributesReadingVisitor.visitEnd(AnnotationAttributesReadingVisitor.java:76)
位于org.springframework.asm.ClassReader.readAnnotationValues(ClassReader.java:2020)
位于org.springframework.asm.ClassReader.accept(ClassReader.java:676)
位于org.springframework.asm.ClassReader.accept(ClassReader.java:527)
位于org.springframework.ide.eclipse.core.java.classreading.JdtConnectedMetadataReader。(JdtConnectedMetadataReader.java:45)
位于org.springframework.ide.eclipse.core.java.classreading.JdtMetadataReaderFactory.getMetadataReader(JdtMetadataReaderFactory.java:53)
位于org.springframework.ide.eclipse.core.java.classreading.CachingJdtMetadataReaderFactory.getMetadataReader(CachingJdtMetadataReaderFactory.java:38)
位于org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig.registerBean(BeansJavaConfig.java:368)
位于org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$2.call(BeansJavaConfig.java:229)
位于org.springframework.ide.eclipse.beans.core.internal.model.BeansJavaConfig$2.call(BeansJavaConfig.java:1)
位于java.util.concurrent.FutureTask.run(未知源)
位于java.util.concurrent.Executors$RunnableAdapter.call(未知源)
位于java.util.concurrent.FutureTask.run(未知源)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源)
位于java.lang.Thread.run(未知源)
原因:java.lang.NoSuchMethodException:org.springframework.context.annotation.Configuration.proxyBeanMethods()
位于java.lang.Class.getDeclaredMethod(未知源)
位于org.springframework.core.annotation.AnnotationUtils$AliasDescriptor。(AnnotationUtils.java:2082)
... 还有22个

我在哪里犯错?如何解决这个问题

问题似乎就在这里。您正在为main类中的RestTemplate实例化一个bean,并且在web服务中再次为该bean创建一个新对象。尝试删除bean定义

删除下面的行

@Bean
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

虽然pom在我看来还不错,但它可能是新旧版本的碰撞。 请注意,spring boot 2.2中添加了
proxyBeanMethods
。 请打开生成的工件,而不要实际使用WinRar之类的工具运行它,并检查BOOT-INF/lib文件夹


确保那里没有旧的spring/spring启动版本。

这是一个更好的答案。。。尽管我没有在
ResponseEntity results=restemplate.exchange(decodedUrl,HttpMethod.GET,null,Object.class)中添加
entity
行。但是在运行代码之后,我得到了相同的错误,所以我将更仔细地检查
pom
文件