四、使用注解(声明式 HttpClient 体验)
Nami 主要是做为 Solon Remoting Rpc 的客户端使用。同时也顺带提供了声明式 http client 的体验能力。
1、接口声明
示例1:默认请求方式(带参数的为 POST;无参数的为 GET)
@NamiClient(url="http://localhost:8080/ComplexModelService/")
public interface IComplexModelService {
//实际请求为:POST http://localhost:8080/ComplexModelService/save
void save(@NamiBody ComplexModel model);
//实际请求为:POST http://localhost:8080/ComplexModelService/read
ComplexModel read(Integer modelId);
}
示例2:调整请求方式和路径
@NamiClient(url="http://localhost:8080/ComplexModelService/", headers="TOKEN=xxx")
public interface IComplexModelService {
//实际请求为:PUT http://localhost:8080/ComplexModelService/save
@NamiMapping("PUT")
void save(@NamiBody ComplexModel model);
//实际请求为:GET http://localhost:8080/ComplexModelService/api/1.0.1?modelId=xxx
@NamiMapping("GET api/1.0.1")
ComplexModel read(Integer modelId);
}
2、接口使用
@Controller
public class Demo{
//注入时没有配置,则使用接口声明时的注解配置
@NamiClient
IComplexModelService complexModelService;
@Mapping
puvlid void test(ComplexModel model){
complexModelService.save(model);
}
}