添加路径+参数来响应字段 [英] Add Path+ Parameters to Response Fields
问题描述
I'm使得Web服务,使用Java和Glassfish的服务器。
I'm还使用Apache服务器进行处理HTTP请求,即,当我提出一个要求,I'm能够得到标准的信息,如:
HTTP / 1.1 200 OK [\\ R] [\\ N]
服务器:GlassFish应用服务器4.1版[\\ R] [\\ N]
的X已启动方式:Servlet的/ JSP 3.1 / 2.3(GlassFish应用服务器4.1版的Java /甲骨文公司/ 1.8)[\\ R] [\\ N]
设置Cookie:JSESSIONID = efc5aa919b55321d3aeaf2c9b3b6;路径= /背景;仅Http [\\ R] [\\ N]
内容类型:文本/ xml的;字符集= UTF-8 [\\ R] [\\ N]
日期:星期四,2015年5月7日15点26分40秒GMT [\\ R] [\\ N]
传输编码:分块[\\ R] [\\ N]
WWW身份验证:基本境界=文件[\\ R] [\\ N]
内容语言:[\\ R] [\\ N]
内容类型:text / html的[\\ R] [\\ N]
内容长度:1090 [\\ R] [\\ N]
SOAPAction报:[\\ R] [\\ N]
主机:本地主机:8080 [\\ R] [\\ N]
连接:保持活动[\\ R] [\\ N]
用户代理:Apache的HttpClient的/ 4.1.1(Java 1.5中)[\\ R] [\\ N]
这是我公司开发的Web应用的一个例子是:
@WebMethod(operationName =someoperation)
@Produces(MediaType.APPLICATION_XML)
公共无效makeHappen(@WebParam(NAME =REQ)obj对象,
@WebParam(NAME =RESP模式= WebParam.Mode.OUT)持有人<串GT;响应){要的信息列表,我得到的,我想增加自己的规范,如:
- > OperationName:someOperation
HTTP / 1.1 200 OK [\\ R] [\\ N]
服务器:GlassFish应用服务器4.1版[\\ R] [\\ N]
的X已启动方式:Servlet的/ JSP 3.1 / 2.3(GlassFish应用服务器4.1版的Java /甲骨文公司/ 1.8)[\\ R] [\\ N]
设置Cookie:JSESSIONID = efc5aa919b55321d3aeaf2c9b3b6;路径= /背景;仅Http [\\ R] [\\ N]
内容类型:文本/ xml的;字符集= UTF-8 [\\ R] [\\ N]
日期:星期四,2015年5月7日15点26分40秒GMT [\\ R] [\\ N]
传输编码:分块[\\ R] [\\ N]
WWW身份验证:基本境界=文件[\\ R] [\\ N]
内容语言:[\\ R] [\\ N]
内容类型:text / html的[\\ R] [\\ N]
内容长度:1090 [\\ R] [\\ N]
SOAPAction报:[\\ R] [\\ N]
主机:本地主机:8080 [\\ R] [\\ N]
连接:保持活动[\\ R] [\\ N]
用户代理:Apache的HttpClient的/ 4.1.1(Java 1.5中)[\\ R] [\\ N]
- 对于WebSphere Application Server:
请参考<一个文档和例子href=\"http://www-01.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.base.doc/ae/twbs_cookiejaxws.html\"相对=nofollow>发送与JAX-WS传输头
下面是说明如何要求一个简短编程示例
传输头是由JAX-WS Web服务客户端发送
应用程序:
块引用>公共类MyApplicationClass {
//注入该服务的端口型的一个实例。
@WebServiceRef(EchoService.class)
私人EchoPortType口; //此方法将调用Web服务操作和发送请求传输头。
公共无效invokeService(){ //设置将包含请求头地图。
地图&LT;弦乐,对象&gt; requestHeaders =新的HashMap&LT;弦乐,对象&gt;();
requestHeaders.put(MyHeader1,这是一个字符串值);
requestHeaders.put(MyHeader2,新的整数(33));
requestHeaders.put(MyHeader3,新的布尔值(true)); //设置为地图上的RequestContext属性。
BindingProvider BP =(BindingProvider)端口;
。bp.getRequestContext()把(com.ibm.websphere.webservices.Constants.REQUEST_TRANSPORT_PROPERTIES,requestHeaders); //调用Web服务操作。
字符串结果= port.echoString(你好,世界!);
}
}
下面是说明如何响应很短的编程示例
传输头是由JAX-WS Web服务端点发送
实现类:
块引用>@WebService
公共类EchoServiceImpl实现EchoServicePortType { //注入WebServiceContext的实例,所以我们可以检索
//将MessageContext的该端点的每次调用。
@Resource
WebServiceContext ctxt; / **
*默认构造函数。
* /
公共EchoServiceImpl(){
....
} 公共字符串echoString(字符串输入){
字符串结果=回声结果是:+输入; //从注入WebServiceContext检索的MessageContext。
MessageContext的MC = ctxt.getMessageContext(); //发送一些头回在响应消息。
地图&LT;弦乐,对象&gt; responseHeaders响应=新的HashMap&LT;弦乐,对象&gt;();
responseHeaders.put(MyHeader1,这是一个字符串响应值);
responseHeaders.put(MyHeader2,新的整数(33));
responseHeaders.put(MyHeader3,新的布尔(假)); //设置了MessageContext的响应头地图。
mc.put(com.ibm.websphere.webservices.Constants.RESPONSE_TRANSPORT_PROPERTIES,responseHeaders响应); 返回结果;
}
}
- 有关GlassFish应用服务器:
您可以得到
javax.xml.ws.WebServiceContext
并从中javax.xml.ws.handler.MessageContext
。然后添加到的MessageContext
你的头,像这样:...
地图&LT;字符串列表&LT;串GT;&GT;标题=新的HashMap&LT;字符串列表&LT;串GT;&GT;();
headers.put(OperationName,someOperation);
messageContext.put(MessageContext.HTTP_REQUEST_HEADERS头)
...您也可以尝试使用这种方法的HTTP标头请求追加:
...
调度&LT;&的SOAPMessage GT;调度=
service.createDispatch(PORTNAME,SOAPMessage.class,Service.Mode.MESSAGE);
地图&LT;字符串列表&LT;串GT;&GT;标题=
新的HashMap&LT;字符串列表&LT;串GT;&GT;();
headers.put(OperationName,someOperation);
dispatch.getRequestContext()。把(MessageContext.HTTP_REQUEST_HEADERS,
头);
...另请参见:
I´m making a Web Service, using Java and Glassfish as server.
I´m also using Apache Server for Processing HTTP requests, i.e, when I make a request, I´m able to get the standard informations, like:
HTTP/1.1 200 OK[\r][\n] Server: GlassFish Server Open Source Edition 4.1 [\r][\n]" X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8)[\r][\n] Set-Cookie: JSESSIONID=efc5aa919b55321d3aeaf2c9b3b6; Path=/context; HttpOnly[\r][\n] Content-Type: text/xml; charset=utf-8[\r][\n] Date: Thu, 07 May 2015 15:26:40 GMT[\r][\n] Transfer-Encoding: chunked[\r][\n] WWW-Authenticate: Basic realm="file"[\r][\n] Content-Language: [\r][\n] Content-Type: text/html[\r][\n] Content-Length: 1090[\r][\n] SOAPAction: ""[\r][\n] Host: localhost:8080[\r][\n] Connection: Keep-Alive[\r][\n] User-Agent: Apache-HttpClient/4.1.1 (java 1.5)[\r][\n]
One Example of a Web Operation that I developed is:
@WebMethod(operationName = "someoperation") @Produces(MediaType.APPLICATION_XML) public void makeHappen(@WebParam(name = "req") Object obj, @WebParam(name = "resp", mode = WebParam.Mode.OUT) Holder<String> response) { To List of information that I get, I want to add own specifications, like: --> OperationName: someOperation HTTP/1.1 200 OK[\r][\n] Server: GlassFish Server Open Source Edition 4.1 [\r][\n]" X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8)[\r][\n] Set-Cookie: JSESSIONID=efc5aa919b55321d3aeaf2c9b3b6; Path=/context; HttpOnly[\r][\n] Content-Type: text/xml; charset=utf-8[\r][\n] Date: Thu, 07 May 2015 15:26:40 GMT[\r][\n] Transfer-Encoding: chunked[\r][\n] WWW-Authenticate: Basic realm="file"[\r][\n] Content-Language: [\r][\n] Content-Type: text/html[\r][\n] Content-Length: 1090[\r][\n] SOAPAction: ""[\r][\n] Host: localhost:8080[\r][\n] Connection: Keep-Alive[\r][\n] User-Agent: Apache-HttpClient/4.1.1 (java 1.5)[\r][\n]
解决方案
- For WebSphere Application Server:
Refer to the documentation and examples in Sending transport headers with JAX-WS
Here is a short programming example that illustrates how request transport headers are sent by a JAX-WS Web services client application:
public class MyApplicationClass { // Inject an instance of the service's port-type. @WebServiceRef(EchoService.class) private EchoPortType port; // This method will invoke the web service operation and send transport headers on the request. public void invokeService() { // Set up the Map that will contain the request headers. Map<String, Object> requestHeaders = new HashMap<String, Object>(); requestHeaders.put("MyHeader1", "This is a string value"); requestHeaders.put("MyHeader2", new Integer(33)); requestHeaders.put("MyHeader3", new Boolean(true)); // Set the Map as a property on the RequestContext. BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(com.ibm.websphere.webservices.Constants.REQUEST_TRANSPORT_PROPERTIES, requestHeaders); // Invoke the web services operation. String result = port.echoString("Hello, world!"); } }
Here is a short programming example that illustrates how response transport headers are sent by a JAX-WS Web services endpoint implementation class:
@WebService public class EchoServiceImpl implements EchoServicePortType { // Inject an instance of WebServiceContext so we can retrieve // the MessageContext for each invocation of this endpoint. @Resource WebServiceContext ctxt; /** * Default constructor. */ public EchoServiceImpl() { .... } public String echoString(String input) { String result = "Echo result: " + input; // Retrieve the MessageContext from the injected WebServiceContext. MessageContext mc = ctxt.getMessageContext(); // Send some headers back in the response message. Map<String, Object> responseHeaders = new HashMap<String, Object>(); responseHeaders.put("MyHeader1", "This is a string response value"); responseHeaders.put("MyHeader2", new Integer(33)); responseHeaders.put("MyHeader3", new Boolean(false)); // Set the response header Map on the MessageContext. mc.put(com.ibm.websphere.webservices.Constants.RESPONSE_TRANSPORT_PROPERTIES, responseHeaders); return result; } }
- For GlassFish Application Server:
You can get the
javax.xml.ws.WebServiceContext
and from itjavax.xml.ws.handler.MessageContext
. Then add to theMessageContext
your headers, something like this:... Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put("OperationName", someOperation); messageContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers) ...
Also you can try to append the HTTP header to the request by using this approach:
... Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put("OperationName", someOperation); dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, headers); ...
See Also:
- How to modify request headers in a J2EE web application
- How to add HTTP header to SOAP Webservice Glassfish
这篇关于添加路径+参数来响应字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!