gpt4 book ai didi

java - 获取 proxyBeanMethods() NosuchMethod 异常

转载 作者:行者123 更新时间:2023-12-02 09:14:53 26 4
gpt4 key购买 nike

我编写了一个 Web 服务来连接到具有自定义 params 的 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;

}

}

主类

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

}

POM.XML

<?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>

当我尝试运行它时,我得到了这个堆栈跟踪。

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

我哪里出错了?如何解决这个问题?

最佳答案

虽然 pom 对我来说看起来不错,但有可能它是新旧 Spring 版本的冲突。请注意,proxyBeanMethods 是在 Spring Boot 2.2 中添加的。请打开生成的工件,而不是使用 WinRar 等工具实际运行它,并检查 BOOT-INF/lib 文件夹。

确保那里没有较旧的 spring/spring-boot 版本。

关于java - 获取 proxyBeanMethods() NosuchMethod 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59082690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com