Hỗ trợ JSON được thêm vào dispatch-servlet.xml (XML đang hoạt động mà không gặp vấn đề gì)?
văn bản nhận xét đã được chỉ thất bại nỗ lực ...Làm cách nào để thêm hỗ trợ JSON bằng XML đã hoạt động?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.example"/>
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.example.domain"/>
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<beans:constructor-arg ref="marshaller"/>
</beans:bean>
<!-- <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:constructor-arg ref="marshaller"/>
</beans:bean> -->
</beans:list>
</beans:property>
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<beans:bean name="note" class="org.springframework.web.servlet.view.xml.MarshallingView">
<beans:constructor-arg ref="marshaller"/>
</beans:bean>
<!-- <beans:bean name="note" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<beans:constructor-arg ref="marshaller"/>
</beans:bean> -->
<!-- InternalResourceViewResolver should be the last sice it always returns/resolves a view -->
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></beans:property>
<beans:property name="prefix" value="/WEB-INF/jsp/"></beans:property>
<beans:property name="suffix" value=".jsp"></beans:property>
</beans:bean>
</beans:beans>
giải pháp bổ sung để: method=RequestMethod.POST, headers = "content-type=application/json"
Bạn vẫn cần:
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter>
<beans:property name="messageConverters">
<beans:list>
<beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
Để ràng buộc một cách chính xác đối tượng java cho @RequestBody.
Wow, có chuyện gì với số phiếu xuống? Ít nhất để lại cho tôi một số con trỏ ... – Solata
Không phải là một kẻ phá hủy, nhưng tôi sẽ tưởng tượng nó không phải là cho chúng tôi biết làm thế nào * thất bại thất bại và những gì các lỗi đã được :) – Affe
Ok, tôi đồng ý, tôi có thể đi nhiều hơn chi tiết ... Câu trả lời của AHungerArtist giúp :) – Solata