YMP在线手册
/ 高级特性—控制器请求处理器
高级特性—控制器请求处理器
在WebMVC模块中除了支持标准Web请求的处理过程,同时也对基于XML和JSON协议格式的请求提供支持,有两种使用场景:
场景一:全局设置,将影响所有的控制器方法;
通过下面的参数配置,默认为default,可选值为[default|json|xml],也可以是开发者自定义的IRequestProcessor接口实现类名称;
ymp.configs.webmvc.request_processor_class=default
场景二:针对具体的控制器方法进行设置;
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/sayHi")
@RequestProcessor(JSONRequestProcessor.class)
public IView sayHi(@RequestParam String name, @RequestParam String content) {
return View.textView("Hi, " + name + ", Content: " + content);
}
@RequestMapping("/sayHello")
@RequestProcessor(XMLRequestProcessor.class)
public IView sayHello(@RequestParam String name, @RequestParam String content) {
return View.textView("Hi, " + name + ", Content: " + content);
}
}通过POST方式向http://localhost:8080/demo/sayHi发送如下JSON数据:
{ "name" : "YMPer", "content" : "Welcome!" }通过POST方式向http://localhost:8080/demo/sayHello发送如下XML数据:
<xml> <name>YMPer</name> <content><![CDATA[Welcome!]]></content> </xml>
以上JSON和XML这两种协议格式的控制器方法,同样支持参数的验证等特性;