Magician-WebMagician 的官方 Web 組件
Magician-Web 是Magician的官方 web組件,可以很方便的管理Controller,支持?jǐn)r截器,會(huì)話管理,注解式參數(shù)校驗(yàn),實(shí)體類接收參數(shù)等
文檔
示例
導(dǎo)入依賴
<!-- This is the jar package build by this project -->
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician-Web</artifactId>
<version>last version</version>
</dependency>
<!-- This is Magician -->
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician</artifactId>
<version>last version</version>
</dependency>
<!-- This is the log package, which supports any package that can be bridged with slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency>
創(chuàng)建核心handler
@HttpHandler(path="/")
public class DemoHandler implements HttpBaseHandler {
@Override
public void request(MagicianRequest magicianRequest, MagicianResponse response) {
try{
// 主要是這句
MagicianWeb.createWeb()
.request(magicianRequest);
} catch (Exception e){
}
}
}
創(chuàng)建 Controller
@Route("/demoController")
public class DemoController {
// You can use entity classes to receive parameters
@Route(value = "/demo", requestMethod = ReqMethod.POST)
public DemoVO demo(DemoVO demoVO){
return demoVO;
}
// You can also directly use MagicianRequest to get parameters
@Route(value = "/demob", requestMethod = ReqMethod.POST)
public String demob(MagicianRequest request){
return "ok";
}
// Download file
@Route(value = "/demob", requestMethod = ReqMethod.POST)
public ResponseInputStream demob(){
ResponseInputStream responseInputStream = new ResponseInputStream();
responseInputStream.setName("file name");
responseInputStream.setBytes(file bytes);
return responseInputStream;
}
}
啟動(dòng) HTTP 服務(wù)
Magician.createHttp()
.scan("handler和controller所在的包名")
.bind(8080);
評(píng)論
圖片
表情
