@RequestMapping( method = RequestMethod.POST) after submit got HTTP Status 405 - Request method 'POST'

Hi I have a wired behavior in this controller. I have two methods Get to prepare information to show in html form and POST to get de submit:

Controller:

@Controller
public class NotificationController {

    final String  JSP_NOTIFICATION_01="pages/sendPush/createNotification";
    final String  JSP_NOTIFICATION_02="pages/sendPush/createNotificationStep2";


    @RequestMapping(value ="/admin/notification/newNotification",method = RequestMethod.GET)
    public String newNotification( Map<String, Object> model, HttpServletRequest request) {

        //prepare  info to fill html form

        request.getSession().setAttribute("notificacion", notification);
        return JSP_NOTIFICATION_01;
    }


    @RequestMapping( value ="/admin/notification/sendNotification", method = RequestMethod.POST)
    public String saveNotification(@ModelAttribute("notForm") SendNotificationModel notForm,
                                      Map<String, Object> model,HttpServletRequest request) {


        //Get all information from HTML form

        System.out.println("llego.."+resultado);

        model.put("resultado", resultado);

        return JSP_NOTIFICATION_02;
    }   
}

JSP

        <form:form action="${pageContext.request.contextPath}/admin/notification/sendNotification" method="post" commandName="notForm">
                <form:hidden path="clientName"  />
                <form:hidden path="clientCode"  />
            </tr>
            <tr>
                <td>topics:</td>
                <td><form:select path="topics" items="${topicList}" /></td>
            </tr>
            <tr>
                <td>users:</td>
                <td><form:select  multiple="true" path="users" items="${userList}"  /></td>
            </tr>
            <tr>
                <td>Tipo de despliege :</td>
                <td><form:select path="tipoNotificacion" items="${tipoNotificacionList}" /></td>
            </tr>

        </table>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Enviar" /></td>
            </tr>
        </table>

    </form:form>

After submit the POST method Receive the request as always, but after the return spring throw 405 error :

HTTP Status 405 - Request method 'POST' not supported
type Status report
message Request method 'POST' not supported
description The specified HTTP method is not allowed for the requested resource.

I'm using Spring 4.1.3 and tomcat8

thanks!!!

Upvotes: 1

Views: 16949

Answers (3)

I fixed adding one method, success (GET). in POST method I put a redirection and works

Reference http://howtodoinjava.com/spring/spring-mvc/spring-mvc-display-validate-and-submit-form-example/

@RequestMapping( value ="/admin/notification/sendNotification", method = RequestMethod.POST)
public String saveNotification(@ModelAttribute("notForm") SendNotificationModel notForm,
                                  Map<String, Object> model,HttpServletRequest request) {

    NotificationDetails newNot = new NotificationDetails();//(NotificationDetails)request.getSession().getAttribute("notificacion");

    String resultado= "Probando" ;
    System.out.println("llego.."+resultado);

    model.put("resultado", resultado);

    return "redirect:/admin/notification/sendNotification/success";
}


@RequestMapping(value = "/admin/notification/sendNotification/success", method = RequestMethod.GET)
public String success(Model model)
{
    return JSP_NOTIFICATION_02;
}

Upvotes: 1

user2290411
user2290411

Reputation: 56

The annotation @RequestMapping can be used both at class and method level. When used at class level it is applied to all the methods. For example see the below code.

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
    private final AppointmentBook appointmentBook;
    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Appointment> get() {
        return appointmentBook.getAppointmentsForToday();
    }
    @RequestMapping(path = "/{day}", method = RequestMethod.POST)
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
        return appointmentBook.getAppointmentsForDay(day);
    }
}

As in the above controller code /appointments is relative and it is applied to all the methods below. Any @RequestMapping at the start of method is added to the relative path.However note that @RequestMapping is not compulsory at class level. Go through the below official documentation for further understanding. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

Upvotes: 1

kuhajeyan
kuhajeyan

Reputation: 11017

remove @RequestMapping(value ="/admin/notification/sendNotification") from controller

Upvotes: 2

Related Questions