@RequestMapping(value="/users/{id}")
public String renderUser(@PathVariable("id") int id){ //do something}@RequestMapping(value="/users/{id}.{format}")
public String renderUserOf(@PathVariable("id") int id,@PathVariable("format") String format){ //do something}第一个 "/users/{id}" 能够正常处理但是第二个 /users/{id}.{format} 却不能正常解析比如 /users/7 会被映射为 "/users/{id}" ,但 /users/7.json 却也被映射为了 "/users/{id}"
1 回答
繁星coding
TA贡献1797条经验 获得超4个赞
Spring根据后缀来决定编组格式.
如果你想返回json.
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <!-- 设置为false开启对Accept Header的支持--> <property name="ignoreAcceptHeader" value="false"/> <!-- 在没有扩展名时的默认形式 --> <property name="defaultContentType" value="application/xml"/> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="jsp" value="text/html"/> <entry key="do" value="text/html"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/> <property name="prefix" value="/WEB-INF/jsp/"></property> </bean> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> </list> </property> <property name="defaultViews"> <list> <!-- for application/xml --> <bean class="org.springframework.web.servlet.view.xml.MarshallingView" > <property name="marshaller"> <ref bean="castorMarshaller"/> </property> </bean> <!-- for application/json --> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </list> </property> </bean>
请求id.xml就得到xml格式的,请求id.json就是json格式的~没有后缀名会根据Accept类型.
- 1 回答
- 0 关注
- 131 浏览
添加回答
举报
0/150
提交
取消