Solon v3.0.4.1

八、头信息的几种设置方式

</> markdown

1、申明时添加

public interface GitHub {
    @NamiMapping(headers={"a=1", "b=2"})
    List<Contributor> contributors(String owner, String repo);
}

2、自己过滤时添加

或者全局过滤时添加,都是基于 Filter 接口

public interface GitHub extends Filter{
    @NamiMapping(headers={"a=1", "b=2"})
    List<Contributor> contributors(String owner, String repo);
    
    @Override
    public Result doFilter(Invocation inv) throws Throwable {
        inv.headers.put(CloudClient.trace().HEADER_TRACE_ID_NAME(), CloudClient.trace().getTraceId());
        return inv.invoke();
    }
}

3、运行时添加(使用 NamiAttachment)

@Controller
public class Demo {
    @NamiClient(url="https://api.github.com")
    GitHub gitHub;

    @Mapping
    public Object test(){
        NamiAttachment.put("a", "1");
        return gitHub.contributors("OpenSolon", "solon");
    }
}