Announcement Announcement Module
Collapse
No announcement yet.
POST and REST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • POST and REST

    Hello friends!

    I am trying to build a solution around REST support under Spring 3.0 M3. So far, I have had no problems using GET method, but I can't find how to process a POST. This is how my method looks:

    Code:
    @RequestMapping(value = "/registry/configureService", method = RequestMethod.POST)
    	public ModelAndView configureService(@RequestBody String instanceName, @RequestBody String className, @RequestBody String options) {
    		log.debug("configuring:" + instanceName + ", " + className + ", " + options);
    		...
    	}
    I understand that, according to the manual, there is the need for MessageConverters. I believe the most common of them are pre-registered by Spring. But even if I register my owns on my context, I still keep getting the error:

    (415) Unsupported Media Type

    This is how I tried to register some message converters:

    Code:
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list>
            <ref bean="stringHttpMessageConverter"/>
            <ref bean="formHttpMessageConverter"/>
          </list>
        </property>
      </bean>
      
      <bean id="formHttpMessageConverter"
        class="org.springframework.http.converter.FormHttpMessageConverter"/>
      
      <bean id="stringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter"/>
    The request body I am posting in this case is a form (Content-Type: application/x-www-form-urlencoded).

    Do you have any idea of what is going wrong?

    Thanks in advance,
    Alex

  • #2
    I found my answer through debugging.

    There is no need to register all those beans. Some basic converters are registered by default, and the one I needed (to convert forms) is there.

    What happens then is that only one @RequestBody can appear per signature. And for HTTP forms, the type of the parameter has to be MultiValueMap, so the signature for my method ends up being:

    Code:
    @RequestMapping(value = "/registry/configureService", method = RequestMethod.POST)
    public ModelAndView configureService(@RequestBody MultiValueMap<String, Object> params) {
    		
    		for (String key : params.keySet()) {
    			log.debug("key: " + key);
    			log.debug("value: " + params.get(key));
    		}
    }
    Maybe this could be better documented. Is either that or I should read better!

    Comment


    • #3
      Can you please tell me how to configure for GET method when useing spring3.0.0.M2

      I have upgraded my porject to user spring3.0.0.M2. First I have started implementing REST support.I have modified my controller code like this.

      @RequestMapping(value="/{userName}", method = RequestMethod.PUT)
      public ModelAndView processPut(
      @PathVariable("userName") String userName,
      @RequestParam("userId")String userId,
      @ModelAttribute("Details") Details details, HttpServletResponse response) {
      .........
      .........
      ModelAndView(ViewNames.USER_DETAILS_SUCCESSVIEW);
      }

      I have removed the bean definitions and mappings in my servlet.xml.

      Then I am getting some url path not found errors.Then I have replaced my mappings in servlet.xml at the time I am getting "Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView "

      Can you please help me out?

      Comment


      • #4
        Originally posted by kavya View Post
        I have upgraded my porject to user spring3.0.0.M2. First I have started implementing REST support.I have modified my controller code like this.

        @RequestMapping(value="/{userName}", method = RequestMethod.PUT)
        public ModelAndView processPut(
        @PathVariable("userName") String userName,
        @RequestParam("userId")String userId,
        @ModelAttribute("Details") Details details, HttpServletResponse response) {
        .........
        .........
        ModelAndView(ViewNames.USER_DETAILS_SUCCESSVIEW);
        }

        I have removed the bean definitions and mappings in my servlet.xml.

        Then I am getting some url path not found errors.Then I have replaced my mappings in servlet.xml at the time I am getting "Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView "

        Can you please help me out?
        Hi,
        I did a mistake.I have removed the definitions in servlet.xml. And it works fine.

        Comment

        Working...
        X