Java Spring Social Twitter-取消引用Twitter对象时的NPE
我使用Spring社交推特v。1.1.0.M4。我成功地启用了与Twitter的内存连接。现在我正在努力保持这些联系。我的数据库连接和日志记录似乎工作正常,因为用户已通过身份验证,我可以看到他们的twitter登录。问题是,我无法访问他们的任何详细信息(粗花呢、跟随等)。我在控制器中显示用户推文的代码是:Java Spring Social Twitter-取消引用Twitter对象时的NPE,java,spring,twitter,spring-social-twitter,Java,Spring,Twitter,Spring Social Twitter,我使用Spring社交推特v。1.1.0.M4。我成功地启用了与Twitter的内存连接。现在我正在努力保持这些联系。我的数据库连接和日志记录似乎工作正常,因为用户已通过身份验证,我可以看到他们的twitter登录。问题是,我无法访问他们的任何详细信息(粗花呢、跟随等)。我在控制器中显示用户推文的代码是: @Autowired private Twitter twitter; @Autowired private TwitterConnectionUtils twitterConnection
@Autowired
private Twitter twitter;
@Autowired
private TwitterConnectionUtils twitterConnectionUtils;
@Autowired
private SessionBean sessionBean;
@RequestMapping("/")
public String index(Model model) {
logger.warning("index");
model.addAttribute("sessionBean", sessionBean);
List<Tweet> tweets = new ArrayList<Tweet>();
try {
if (!twitterConnectionUtils.isConnectedToTwitter()) {
return "index";
}
tweets.addAll(twitter.timelineOperations().getHomeTimeline());
} catch (Exception e) {
logger.warning(ExceptionUtils.getStackTrace(e));
model.addAttribute("exception", e.toString());
return "index";
}
model.addAttribute("tweets", tweets);
return "twitter/tweets";
}
你知道是什么导致了这个问题吗?我还提供了库的javaconfig,以供参考:
@Configuration
@EnableTwitter(appId="myAppId", appSecret="myAppSecret")
public class TwitterConfig {
@Autowired
private TwitterConnectInterceptor twitterConnectInterceptor;
@Inject
private DataSource dataSource;
@Inject
private Environment environment;
@Bean
public UserIdSource userIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
return "myTestingPurposesLogin"; // TODO change to security-obtained user login
}
};
}
@Bean
public ConnectController connectController() {
CustomConnectController connectionController =
new CustomConnectController(connectionFactoryLocator(), connectionRepository());
connectionController.addInterceptor(twitterConnectInterceptor);
return connectionController;
}
@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new TwitterConnectionFactory(
"myAppId", "myAppSecret"));
return registry;
}
@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return usersConnectionRepository().createConnectionRepository(authentication.getName());
}
@Bean
public UsersConnectionRepository usersConnectionRepository() {
return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator(),
textEncryptor());
}
@Bean
public TextEncryptor textEncryptor() {
return Encryptors.noOpText();
}
}
改变
例如(伪代码,我不知道返回的所有类型):
这将告诉您哪个方法调用没有返回对象。好吧,我完全错了!下面是一个易于遵循的工作示例:
@Configuration
@EnableTwitter(appId="myAppId", appSecret="myAppSecret")
@EnableJdbcConnectionRepository
public class TwitterConfig {
@Autowired
private TwitterConnectInterceptor twitterConnectInterceptor;
@Bean
public UserIdSource userIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
return "myTestingPurposesLogin";
}
};
}
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
CustomConnectController connectionController = new CustomConnectController(connectionFactoryLocator, connectionRepository);
connectionController.addInterceptor(twitterConnectInterceptor);
return connectionController;
}
@Bean
public TextEncryptor textEncryptor() {
return Encryptors.noOpText();
}
}
@EnableJdbcConnectionRepository
注释对于让我走上正确的道路至关重要。这些注释中没有一个是null
(技术上很可能)。问题在于某些方面。不,您可以使用带有代理的AOP将建议应用于某些连接点。在处理过程中失败了。Robert,你的代码将在第一行抛出异常。正如我所说的,任何对twitter的取消引用都会在一些内部代理中导致NPE。您有任何自定义方面吗?做任何AOP?不,我没有明确做任何AOP。
tweets.addAll(twitter.timelineOperations().getHomeTimeline());
var timelineOperations = twitter.TimelineOperations();
var homeTimeline = timelineOperations.GetHomeTimeline();
tweets.addAll(homeTimeline);
@Configuration
@EnableTwitter(appId="myAppId", appSecret="myAppSecret")
@EnableJdbcConnectionRepository
public class TwitterConfig {
@Autowired
private TwitterConnectInterceptor twitterConnectInterceptor;
@Bean
public UserIdSource userIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
return "myTestingPurposesLogin";
}
};
}
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
CustomConnectController connectionController = new CustomConnectController(connectionFactoryLocator, connectionRepository);
connectionController.addInterceptor(twitterConnectInterceptor);
return connectionController;
}
@Bean
public TextEncryptor textEncryptor() {
return Encryptors.noOpText();
}
}