Results 1 to 9 of 9

Thread: using @requestbody for Jsons request not working saying "Method not allowed"

  1. #1

    Default using @requestbody for Jsons request not working saying "Method not allowed"

    HI,
    I have Spring applications built where in I am able to retrieve the response when using simple request parameters in the request coming from UI.However when trying to replace it with @RequestBody with passing a json in the request.It gives me 405 "Method not allowed error".
    Here is the code of web.xml file
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>

    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListe ner
    </listener-class>
    </listener>

    spring_servlet.xml

    <context:component-scan base-package="com.dataguise.dgcontrol.servlets" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
    <list>

    <ref bean="jsonConverter" />
    </list>
    </property>
    </bean>

    <bean class="org.springframework.web.servlet.view.json.M appingJacksonJsonView">
    <property name="contentType" value="application/json"/>
    </bean>

    <bean id="jsonConverter"
    class="org.springframework.http.converter.json.Map pingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    </bean>

    My Spring controller class has two methods
    @Controller


    public class DgCentralControllerServlet {

    DgControlRepository repo = new DgControlRepository();


    ///Method 1
    @RequestMapping(value ="/LoadDiscoverExpression")
    public @ResponseBody List<DgPolicyRegexPatternsUI> getMovie(@RequestParam("id") Integer id ,@RequestParam("source") String source, HttpServletResponse response,
    HttpServletRequest request) throws DgException{

    List<DgPolicyRegexPatternsUI> dgRegexPatternsList = repo.loadPolicyRegexPatternsTest(id,source, "All");

    return dgRegexPatternsList;
    }

    /// Method 2
    @RequestMapping(value ="/LoadDiscover", method=RequestMethod.POST, headers={"Accept=*/*", "Content-Type=application/json"})
    public @ResponseBody DgDiscoverExpressionPol getexp(@RequestBody DgDiscoverExpressionPol pol,Model model) throws Exception{

    DgDiscoverExpressionPol returnpol = new DgDiscoverExpressionPol();
    returnpol.setSource(pol.getSource());
    return returnpol;
    }

    Method 1 works and gives me response when running the URL on the browser .using RESTCLIENT addon on mozilla
    http://localhost:8080/dgControl/Load...rce=structured

    However when using the Method 2 on the the RESTCLIENT (add on on mozilla) to give POST request
    http://localhost:8080/dgControl/LoadDiscover
    with the request body as json={"source":"structured"}

    It gives me 405 Method not allowed error

    Is there any other classes I need to create for json conversion to Objects to me implemented?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Please use [ code][/code ] tags when posting code/xml/stacktraces, that way it remains readable...

    Don't use the ion.AnnotationMethodHandlerAdapter when using Spring 3.1 or up. Remove it, jackson will be setup for you automatically by Spring 3.1 and up when it detects the classes.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    HI,
    Thanks for the reply.Removing the AnnotationMethodhandlerAdapter didnt help.Infact this time the error I saw in tomcat console logs were
    Code:
    java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.isConcrete()Z
        at org.codehaus.jackson.map.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:80)
        at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:266)
        at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:198)
        at org.codehaus.jackson.map.deser.StdDeserializerProvider.hasValueDeserializerFor(StdDeserializerProvider.java:152)

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Looks like a jackson version error or jar conflict.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    I noticed that I was using @enableWebMvc annotation in my @controller class.Removing this annotation solved the above issue.But the "Method not allowed" error is still there.In fact when seeing the detail explanation of the error in the REST client , it says
    Code:
    The specified HTTP method is not allowed for the requested resource (Request method 'POST' not supported).
    where in I have specified the method definition as POST
    Code:
    @RequestMapping(value ="/LoadDiscover", method=RequestMethod.POST, headers={"Accept=*/*", "Content-Type=application/json"})
    I am not sure where m I missing things
    Last edited by Marten Deinum; Feb 27th, 2013 at 01:14 AM. Reason: Fixed code tags

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Enable trace logging to see what is happening inside spring and why the request isn't matched. I would start by removing the headers part and see if that makes it work.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    Hey, Thanks a ton..I enabled the log trace and this is what I received
    Code:
    2013-02-27 16:03:02,984 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.FrameworkServlet:processRequest(643) | Bound request context to thread: org.apache.catalina.connector.RequestFacade@1ea07a3 
    2013-02-27 16:03:03,020 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.DispatcherServlet:doService(690) | DispatcherServlet with name 'spring' processing POST request for [/dgControl/LoadDiscover] 
    2013-02-27 16:03:03,083 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.DispatcherServlet:getHandler(939) | Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@468fa3] in DispatcherServlet with name 'spring' 
    2013-02-27 16:03:03,119 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.handler.AbstractUrlHandlerMapping:getHandlerInternal(219) | Mapping [/LoadDiscover] to handler 'com.dataguise.dgcontrol.servlets.DgCentralControllerServlet@192a63a' 
    2013-02-27 16:03:03,151 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.DispatcherServlet:getHandlerAdapter(976) | Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@ce0284] 
    2013-02-27 16:03:03,216 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:resolveException(106) | Resolving exception from handler [com.dataguise.dgcontrol.servlets.DgCentralControllerServlet@192a63a]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 
    2013-02-27 16:03:03,296 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:resolveException(106) | Resolving exception from handler [com.dataguise.dgcontrol.servlets.DgCentralControllerServlet@192a63a]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 
    2013-02-27 16:03:03,336 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver:resolveException(106) | Resolving exception from handler [com.dataguise.dgcontrol.servlets.DgCentralControllerServlet@192a63a]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 
    2013-02-27 16:03:03,366 |  WARN | 	[http-8080-1] |org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver:handleHttpRequestMethodNotSupported(166) | Request method 'POST' not supported 
    2013-02-27 16:03:03,401 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.DispatcherServlet:doDispatch(805) | Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling 
    2013-02-27 16:03:03,431 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.FrameworkServlet:processRequest(670) | Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1ea07a3 
    2013-02-27 16:03:03,456 | DEBUG | 	[http-8080-1] |org.springframework.web.servlet.FrameworkServlet:processRequest(677) | Successfully completed request 
    2013-02-27 16:03:03,488 | DEBUG | 	[http-8080-1] |org.springframework.context.support.AbstractApplicationContext:publishEvent(299) | Publishing event in WebApplicationContext for namespace 'spring-servlet': ServletRequestHandledEvent: url=[/dgControl/LoadDiscover]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[null]; user=[null]; time=[499ms]; status=[OK] 
    2013-02-27 16:03:03,521 | DEBUG | 	[http-8080-1] |org.springframework.context.support.AbstractApplicationContext:publishEvent(299) | Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/dgControl/LoadDiscover]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[spring]; session=[null]; user=[null]; time=[499ms]; status=[OK]
    Trying to see where did I miss the handler

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    It appears to be still debug nog trace. Which spring version are you using?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9

    Default

    I am using spring 3.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •