Advanced Features—Controller Request Handler


In addition to supporting standard Web request processing, the WebMVC module also provides support for requests based on XML and JSON protocol formats. There are two usage scenarios:

Scenario 1 : Global setting, which will affect all controller methods;

is configured through the following parameters, the default is default, the optional value is [default|json|xml], or it can be customized by the developer IRequestProcessor interface implementation class name;

ymp.configs.webmvc.request_processor_class=default

Scenario 2: Set for specific controller methods;

@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);
    }
}

Send the following JSON data to http://localhost:8080/demo/sayHi via POST:

{ "name" : "YMPer", "content" : "Welcome!" }

Send the following XML data to http://localhost:8080/demo/sayHello via POST:

<xml>
    <name>YMPer</name>
    <content><![CDATA[Welcome!]]></content>
</xml>

The controller methods of the above two protocol formats, JSON and XML, also support parameter verification and other features;