4
votes

I'm trying to override the default behavior of Spring Social to redirect to "connect/{providerId}Connected" once connected to a provider (Twitter, Facebook etc).

So I'm trying to override the default behavior by overriding the method protected java.lang.String connectedView(java.lang.String providerId)

So I've subclassed ConnectController and tried overriding:

@Controller
public class CustomConnectController extends ConnectController{

    @Inject
    public CustomConnectController(
            ConnectionFactoryLocator connectionFactoryLocator,
            ConnectionRepository connectionRepository) {
        super(connectionFactoryLocator, connectionRepository);
    }


    @Override
    protected String connectedView(String providerId){
        //Do some logic

        return "redirect:/foo/bar;
    }

}

See documentation of the controller class: http://static.springsource.org/spring-social/docs/1.0.x/api/org/springframework/social/connect/web/ConnectController.html

But I get the following error:

Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'org.springframework.social.connect.web.ConnectController#0' bean method public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.connect(java.lang.String,org.springframework.web.context.request.NativeWebRequest) to {[/connect/{providerId}],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'customConnectController' bean method public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.connect(java.lang.String,org.springframework.web.context.request.NativeWebRequest) mapped.

Can anyone please guide. My requirement is as follows: 1. After user connects social account (Twitter, Facebook etc) 2. Do some business logic 3. Redirect to /foo/bar page

Please help.

2

2 Answers

7
votes

Ok, I found the solution myself. Posting the answer for everyone, so that anyone struggling with the silly thing might be benefited:

Actually I had ConnectController configured in my Config & now the custom controller was stepping on that & hence it says already mapped. Removing the config from the config solves the problem.

In my case removing the following code:

<bean class="org.springframework.social.connect.web.ConnectController">
        relies on by-type autowiring for the constructor-args
        <property name="applicationUrl" value="${application.url}" />
    </bean> 
0
votes

Well You have done everything right but have forgotten to add

    @RequestMapping("/connect")

In case you are working with Spring boot and doesn't have and spring-config.xml files you can use below code.

This code did the trick for me

@Controller
@RequestMapping("/connect")
public class ChangeDefaultFlowController extends ConnectController {

    public ChangeDefaultFlowController(ConnectionFactoryLocator connectionFactoryLocator,
            ConnectionRepository connectionRepository) {
        super(connectionFactoryLocator, connectionRepository);
    }

    @Override
    protected String connectedView(String providerId) {
        return "redirect:/"+providerId;
    }

}

This Link offers more explanation on changing the redirect flow to page or another rest controller