gpt4 book ai didi

java - 将路径+参数添加到响应字段

转载 作者:行者123 更新时间:2023-12-01 11:34:53 25 4
gpt4 key购买 nike

我正在制作一个 Web 服务,使用 Java 和 Glassfish 作为服务器。

我还使用 Apache Server 来处理 HTTP 请求,即,当我发出请求时,我能够获取标准信息,例如:

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]

我开发的 Web 操作的一个示例是:

@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]

最佳答案

  • 对于 WebSphere 应用程序服务器:

请参阅 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;
}
}
  • 对于 GlassFish 应用服务器:

您可以获取javax.xml.ws.WebServiceContext并从中获取javax.xml.ws.handler.MessageContext。然后将 header 添加到 MessageContext 中,如下所示:

...
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("OperationName", someOperation);
messageContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers)
...

您还可以尝试使用以下方法将 HTTP header 附加到请求中:

...
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);
...

另请参阅:

关于java - 将路径+参数添加到响应字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30105806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com