@RequestMapping(value = "/query", method = RequestMethod.GET)
public void queryFoodsByName(String foodName, OutputStream os)
throws UnsupportedEncodingException, IOException {
String JSONString = service.queryFoodsByName(foodName);
os.write(JSONString.getBytes("UTF-8"));
}
@ResponseBod
@RequestMapping(value = "/queryFood", method = RequestMethod.GET)
public byte[] queryFoodsByName_jsonStr(String foodName)
throws UnsupportedEncodingException{
String JSONString = service.queryFoodsByName(foodName);
return JSONString.getBytes("UTF-8");
}
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 配置响应支持的类型 -->
<value>text/html</value>
<!-- 设置请求体支持的类型 -->
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
测试方法:
@ResponseBody
@RequestMapping(value = "/queryFoodList", method = RequestMethod.GET)
public List<Food> queryFoodsList(String foodName) throws UnsupportedEncodingException{
List<Food> list = service.queryFoodsList(foodName);
return list;
}
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 配置响应支持的类型 -->
<value>text/html</value>
<!-- 设置请求体支持的类型 -->
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>