java.lang.NoSuchMethodError在使用spring-social-twitter时发生

huangapple go评论76阅读模式
英文:

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: &#39;org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()&#39;
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(
&quot;&lt;cosumerKey&gt;&quot;,
&quot;&lt;consumerSecret&gt;&quot;,
&quot;&lt;accessToken&gt;&quot;,
&quot;&lt;accessTokenSecret&gt;&quot;);
}
}

TwitterController:

@RestController
@RequestMapping((&quot;/api&quot;))
public class TwitterController {
@Autowired
private TwitterTemplateCreator twitterTemplateCreator;
@PostMapping(value = &quot;/send&quot;)
public void send(@RequestParam(&quot;name&quot;) String name, @RequestParam(&quot;text&quot;) String text) {
twitterTemplateCreator.getTwitterTemplate().directMessageOperations().sendDirectMessage(name, text);
}
}

And finally pom.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;3.0.2&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;twitter-test&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;twitter-test&lt;/name&gt;
&lt;description&gt;twitter-test&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;17&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.social&lt;/groupId&gt;
&lt;artifactId&gt;spring-social-twitter&lt;/artifactId&gt;
&lt;version&gt;1.1.0.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;excludes&gt;
&lt;exclude&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;/exclude&gt;
&lt;/excludes&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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:

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.social&lt;/groupId&gt;
&lt;artifactId&gt;spring-social-core&lt;/artifactId&gt;
&lt;version&gt;1.1.6.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.social&lt;/groupId&gt;
&lt;artifactId&gt;spring-social-web&lt;/artifactId&gt;
&lt;version&gt;1.1.6.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.social&lt;/groupId&gt;
&lt;artifactId&gt;spring-social-security&lt;/artifactId&gt;
&lt;version&gt;1.1.6.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

Then create new temlate which extends AbstractOAuth1ApiBinding:

public class TwitterTemplate extends AbstractOAuth1ApiBinding {
public static final String TWITTER_API = &quot;https://api.twitter.com/1.1&quot;;
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 + &quot;/users/show.json?screen_name={name}&quot;, TwitterUserResponse.class, name);
}
}

Then just instantiate TwitterTemplate and call needed method.

huangapple
  • 本文由 发表于 2023年2月9日 01:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定