英文:
java.lang.NoSuchMethodError when using spring-social-twitter
问题
I try using Twitter API via Spring library spring-social-twitter
. However I'm when calling sendDirectMessage(String toScreenName, String text)
function I get the following exception:
at org.springframework.social.twitter.api.impl.TwitterErrorHandler.handleError(TwitterErrorHandler.java:56) ~[spring-social-twitter-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:804) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:503) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.social.twitter.api.impl.DirectMessageTemplate.sendDirectMessage(DirectMessageTemplate.java:77) ~[spring-social-twitter-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at com.example.twittertest.TwitterController.send(TwitterController.java:17) ~[classes/:na]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731) ~[tomcat-embed-core-10.1.5.jar:6.0]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
I have the my TwitterTemplate
configured as following:
@Component
public class TwitterTemplateCreator {
public Twitter getTwitterTemplate() {
return new TwitterTemplate(
"<cosumerKey>",
"<consumerSecret>",
"<accessToken>",
"<accessTokenSecret>");
}
}
TwitterController
:
@RestController
@RequestMapping("/api")
public class TwitterController {
@Autowired
private TwitterTemplateCreator twitterTemplateCreator;
@PostMapping(value = "/send")
public void send(@RequestParam("name") String name, @RequestParam("text") String text) {
twitterTemplateCreator.getTwitterTemplate().directMessageOperations().sendDirectMessage(name, text);
}
}
And finally 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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>twitter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>twitter-test</name>
<description>twitter-test</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-twitter</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
My guess is that there is inconsistency with dependencies versions but after trying different Spring Boot versions I still can't figure out what causes this issue.
英文:
I try using Twitter API via Spring library spring-social-twitter
. However I'm when calling sendDirectMessage(String toScreenName, String text)
function I get the following exception:
java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()'
at org.springframework.social.twitter.api.impl.TwitterErrorHandler.handleError(TwitterErrorHandler.java:56) ~[spring-social-twitter-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:804) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:503) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.social.twitter.api.impl.DirectMessageTemplate.sendDirectMessage(DirectMessageTemplate.java:77) ~[spring-social-twitter-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at com.example.twittertest.TwitterController.send(TwitterController.java:17) ~[classes/:na]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731) ~[tomcat-embed-core-10.1.5.jar:6.0]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
I have the my TwitterTemplate
configured as following:
@Component
public class TwitterTemplateCreator {
public Twitter getTwitterTemplate() {
return new TwitterTemplate(
"<cosumerKey>",
"<consumerSecret>",
"<accessToken>",
"<accessTokenSecret>");
}
}
TwitterController
:
@RestController
@RequestMapping(("/api"))
public class TwitterController {
@Autowired
private TwitterTemplateCreator twitterTemplateCreator;
@PostMapping(value = "/send")
public void send(@RequestParam("name") String name, @RequestParam("text") String text) {
twitterTemplateCreator.getTwitterTemplate().directMessageOperations().sendDirectMessage(name, text);
}
}
And finally 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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>twitter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>twitter-test</name>
<description>twitter-test</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-twitter</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
My guess is that there is inconsistency with dependencies versions but after trying different Spring Boot versions I still can't figure out what causes this issue.
答案1
得分: 1
您的猜测是正确的。Spring Social Twitter 不再维护。最后一个发布版本为1.1.2.RELEASE,发布于2015年10月,依赖于Spring Framework 4。Spring Boot 1.x 基于Spring Framework 4.x,但自2019年以来已经停止支持。
我建议您寻找Spring Social Twitter的替代方案。一种替代方法是将其与Spring Boot 1.x一起使用,但这将意味着您正在使用不再得到支持的软件,无法获得错误修复。即使您愿意承担这种风险,Spring Social Twitter 也有很大可能不兼容当前版本的Twitter API。
英文:
Your guess is correct. Spring Social Twitter is no longer maintained. The last release, 1.1.2.RELEASE, was made in October 2015 and it depends on Spring Framework 4. Spring Boot 1.x is based on Spring Framework 4.x but it has been out of support since 2019.
I would recommend that you find an alternative to Spring Social Twitter. The alternative would be to use it with Spring Boot 1.x but then you will be using unsupported software for which bug fixes won't be available. Even if you're happy to take that risk, there's also a good chance that Spring Social Twitter isn't compatible with the current version of Twitter's API.
答案2
得分: 0
通过使用spring-social
定义自己的OAuth1 Rest Template 来解决了问题。首先,包括以下依赖项:
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
然后创建一个扩展AbstractOAuth1ApiBinding
的新模板:
public class TwitterTemplate extends AbstractOAuth1ApiBinding {
public static final String TWITTER_API = "https://api.twitter.com/1.1";
public TwitterTemplate(String consumerKey, String consumerSecret, String accessToken,
String accessTokenSecret) {
super(consumerKey, consumerSecret, accessToken, accessTokenSecret);
}
public TwitterUserResponse getUser(String name) {
return getRestTemplate().getForObject(
TWITTER_API + "/users/show.json?screen_name={name}", TwitterUserResponse.class, name);
}
}
然后只需实例化TwitterTemplate
并调用所需的方法。
英文:
Solved by defining my own OAuth1 Rest Template using spring-social
.
First included following dependencies:
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
Then create new temlate which extends AbstractOAuth1ApiBinding
:
public class TwitterTemplate extends AbstractOAuth1ApiBinding {
public static final String TWITTER_API = "https://api.twitter.com/1.1";
public TwitterTemplate(String consumerKey, String consumerSecret, String accessToken,
String accessTokenSecret) {
super(consumerKey, consumerSecret, accessToken, accessTokenSecret);
}
public TwitterUserResponse getUser(String name) {
return getRestTemplate().getForObject(
TWITTER_API + "/users/show.json?screen_name={name}", TwitterUserResponse.class, name);
}
}
Then just instantiate TwitterTemplate
and call needed method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论