How To Get MessageContext In Service Implementation Class In JAX-WS

MessageContext is used to get almost all type of info associated with current SOAP request. In JAX-WS, this how you can get MessageContext in your service implementation class:

@WebService
public class TestingService {
@Resource
WebServiceContext wsc;

@WebMethod
public String sayHi(String name) {
MessageContext mc = wsc.getMessageContext();
}
}

If you are thinking who set the WebServiceContext in our class, its runtime. Before the SOAP method is called. The runtime inject the WebServiceContext identifying by @Resource annotation. And from this WebServiceContext, we get MessageContext.

Comments