lmcadory
lmcadory

Reputation: 2294

Spring MVC NoSuchRequestHandlingMethodExecption

I've searched through here but I can't find anything that solves my strange problem. I inherited some code that's using Spring MVC, Tiles, JSP. I'm trying to convert the code to use a more ReST API. So far I've been able to convert some GET requests to send JSON, but POST doesn't seem to be working.

I have a simple post mapping in my controller:

@RequestMapping(value = "/save", method = RequestMethod.POST, headers = MediaType.APPLICATION_JSON)
public void save(@RequestBody List<Map<String, String>> test){
  Map<String, String> input = Maps.newHashMap();
  for(Map<String, String> x : test){
     input.putAll(i);
  }
  System.out.println(input);
}

I have the standard web.xml that uses DispatcherServlet and the mapping is /

My mvc config xml is:

 <!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.unibet.livebannerservice"/>
<context:annotation-config/>

<!-- Basic MVC config -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/"/>


<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="defaultContentType" value="text/html"/>
        <property name="ignoreAcceptHeader" value="true"/>
        <property name="favorPathExtension" value="true"/>
        <property name="order" value="1"/>
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                <!-- Use tiles2 for views -->
                <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <ref bean="jsonView"/>
            </list>
        </property>
    </bean>
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="contentType" value="application/json;charset=UTF-8"/>        
</bean>
<bean id="tilesConfigurer"
      class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/jsp/tiles/tiles.xml</value>                
        </list>
    </property>
    <property name="checkRefresh" value="true" />
</bean>
</beans>

On the frontend I have a simple button that makes and ajax post request to the server.

$.ajax({
url: 'mypath/save',
contentType: 'applicatoin/json',
type: 'POST',
data: JSON.stringify({name:'John',value:'Doe'}),
success: function(data){
console.log(data);
});

Whenever I try to post I get and

org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/mypath/save', method: 'POST', parameters map['{"key":"John","value":"Doe"}' -> array[']]]

I'm lost. My next step is to just start from scratch and create the ReST api from a simple tutorial and then try and bring tiles back into the fold.

Upvotes: 0

Views: 413

Answers (1)

Qianyue
Qianyue

Reputation: 1777

Firstly, I have a question for your url. In ajax it is : "mypath/save" and in Java it is : "/save". It shouldn't be a problem but it may be.

Secondly, your json object is something like :

{
    "name":"John", "value":"Doe"
}

It is not a data structure of Map. Maybe you should create a class of something like :

class user {
    String name;
    String value;
}

Hope help!

Upvotes: 1

Related Questions