@RequestMapping("jsp/test")
public class TestSpring {
protected final Logger log = LoggerFactory.getLogger(getClass());
@RequestMapping(params = "method=getjson")
@ResponseBody
public String test1(){
EaiBrand brand =new EaiBrand();
brand.setBrandCode("brandCode");
brand.setBrandName("brandName");
brand.setEaiBrandId(12L);
brand.setFailCount(2);
return JSON.toJSONString(brand);
}
@RequestMapping(params = "method=getjson2")
@ResponseBody
public EaiBrand test2(){
EaiBrand brand =new EaiBrand();
brand.setBrandCode("brandCode");
brand.setBrandName("brandName");
brand.setEaiBrandId(12L);
brand.setFailCount(2);
return brand;
}
/**
*
* restful風格測試. <br/>
*
* @author haibing.xiao
* @date: 2015年4月24日 下午3:57:43
* @version 1.0
*
* @param ownerId
* @return
*/
@RequestMapping(value="/{ownerId}")
@ResponseBody
public EaiBrand test3(@PathVariable String ownerId){
log.info("ownerId={}",ownerId);
EaiBrand brand =new EaiBrand();
brand.setBrandCode("brandCode");
brand.setBrandName("brandName");
brand.setEaiBrandId(12L);
brand.setFailCount(2);
brand.setBrandName(ownerId);
return brand;
}
}
spring-mvc.xml配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!--返回字符串格式json-->
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>