我想使用Spring 3.x發送json到服務器,我使用註解@RequestBody,但是我的控制器沒有調用。發送json到服務器彈簧3.x
請給我一個完整的例子,如果有的話,我試圖使用 http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/的例子,但它不起作用。這裏是一個JavaScript代碼
function sendAjax() {
var person = new Object();
person.firstname = "Firsname";
person.lastname = "Lastname";
jQuery.ajax({
url: "person",
type: 'POST',
dataType: 'json',
data: person,
contentType: 'application/json',
success: function(data) {
alert(data.firstname + " " + data.lastname);
}
});}
我的控制器是
@Controller public class AjaxController {
@RequestMapping(value="person", method = RequestMethod.POST)
public @ResponseBody Person getRequest(@RequestBody Person person) {
System.out.println(person.getFirstname() + " " + person.getLastname());
return new Person("Return", "Body");
}}
我的XML文件
<mvc:annotation-driven />
<context:component-scan base-package="com.synisys.spring.test.controllers" />
<context:annotation-config/>
<mvc:resources mapping="/js/**" location="/js/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
會發生什麼?你有錯誤嗎?有沒有日誌輸出? –
什麼都沒有發生。我的控制器只是不叫。如果我刪除我的控制器中的@RequestBody,它的工作原理 – user687014