java - 不支持 Spring Social/connect/twitter 请求方法 'GET'

标签 java spring-mvc spring-boot spring-social

我正在关注 http://spring.io/guides/gs/accessing-twitter/使用 Spring Boot 为 twitter 设置 spring social 但是当我运行应用程序并重定向到 localhost:8080/connect/twitter 我得到:

There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported

我知道这与 ConnectController 的映射有关,我该如何真正解决这个问题?

@Controller
public class HelloTwitterController {
    private final Twitter twitter;
    private final ConnectionRepository connectionRepo;

    @Inject
    public MessoTwitterController(Twitter twitter, ConnectionRepository connectionRepo) {
        this.twitter = twitter;
        this.connectionRepo = connectionRepo;
    }

    @RequestMapping(value ="/", method = RequestMethod.GET)
    public String welcomeTwitter (Model model) {
        if (connectionRepo.findPrimaryConnection(Twitter.class) == null)
            return "redirect:/connect/twitter";

        model.addAttribute(twitter.userOperations().getUserProfile());
        model.addAttribute("friends", twitter.friendOperations().getFriends());
        return "welcomeTwitter";
    }
}

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.mypackage")
public class MyApplication {
       public static void main(String[] args) {
           SpringApplication.run(MyApplication.class, args);
    }
}

最佳答案

您的代码中没有 View 解析器,因此无法呈现 View (返回的字符串)。

您必须添加您的 View 解析器(例如 thymeleaf、freemarker、jsp)或将此行放入 application.properties

spring.social.auto_connection_views=true

这将使用默认模板呈现您的 View 。

关于java - 不支持 Spring Social/connect/twitter 请求方法 'GET',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396544/

相关文章:

java - Spring Security 自定义身份验证 - AuthenticationProvider 与 UserDetailsS​​ervice

ios - Spring Mobile 能否区别对待 iOS 和 Android?

java - Spring Boot gradle 插件不起作用

spring-boot - 在 Spring 中,事务性是否可以扩展到其他操作而不是数据库?

java - Apache camel,一条通往 "join"2条路线的路

java - Oracle Java KeyStore 实现是线程安全的吗?

java - 将多个对象写入包

java - Java中HashMap的解析

java - Spring,显示异常或 404 发生错误的 View

java - Spring Boot应用程序中的sessionFactory不会创建