paulc4 / microservices-demo

Demo application to go with Blog on spring.io
889 stars 830 forks source link

405 METHOD_NOT_ALLOWED #9

Closed baristabak closed 6 years ago

baristabak commented 8 years ago

Hi Paul, I applied your demo and it worked great but I am getting 405 METHOD_NOT_ALLOWED exception when I am trying to send a POST request. I changed the code like below:

In WebAccountsController class:

FROM

@RequestMapping(value = "/accounts/search", method = RequestMethod.GET)
    public String searchForm(Model model) {
        model.addAttribute("searchCriteria", new SearchCriteria());
        return "accountSearch";
    }

TO

    @RequestMapping(value = "/accounts/search", method = RequestMethod.POST)
    @ResponseStatus(value=HttpStatus.OK)
    @ResponseBody
    public String searchForm(@ModelAttribute SearchCriteria searchCriteria, Model model) {
        model.addAttribute("searchCriteria", searchCriteria);
        return "accountSearch";
    }

In WebAccountsService class:

FROM

    public Account findByNumber(String accountNumber) {

        logger.info("findByNumber() invoked: for " + accountNumber);
        return restTemplate.getForObject(serviceUrl + "/accounts/{number}",
                Account.class, accountNumber);
    }   

TO

    public Account findByNumber(String accountNumber) {

        logger.info("findByNumber() invoked: for " + accountNumber);
        return restTemplate.postForObject(serviceUrl + "/accounts/",accountNumber,Account.class);
    }

and in accountSearch.html file:

FROM

    <form action="#" th:object="${searchCriteria}" method="GET"
        th:action="@{/accounts/dosearch}" class="form-horizontal">

TO

    <form action="#" th:object="${searchCriteria}" method="POST" 
        th:action="@{/accounts/dosearch}" class="form-horizontal">

What else should I change? Could you please help me?

paulc4 commented 8 years ago

At first glance your code examples look right. Nothing obviously wrong.

I suggest you run up an intermediate HTTP Proxy so you can see the HTTP requests and responses. If you use Eclipse/STS, try using the TCP/IP Monitor (Window -> Show View -> Debug). Don't forget to Click Start once you've configured it (I always do!). Netbeans and IntelliJ have something similar - see here: http://stackoverflow.com/questions/6941430/is-there-any-http-monitor-for-intellij-idea-gui-like-the-one-in-netbeans.