我正在使用「Java EE 1.5,支持JAX-WS批註來創建Web服務。Java EE 1.5 Servlet - http方法OPTIONS給出了「405方法不允許」
我的網絡服務可以調用沒有肥皂用戶界面的問題。 我想從Chrome使用JavaScript和JQuery調用它。我遇到了交叉視線安全問題。我是需要添加到呼叫中的標頭的一部分,並且我添加了它們並驗證它們存在於Soap UI中。 看着鉻日誌我可以看到,它正在發送一個http調用的方法「選項」到web服務,它得到一個響應「405(方法不允許)」
從我能做些什麼在它到達我的servlet之前攔截「OPTIONS」方法。 「POST」請求無任何問題。 有誰知道如何配置Java EE 1.5 servlett以接收OPTIONS HTTP方法?我用google搜索過,但沒有運氣。
我的web.xml:
<?xml version = '1.0' encoding = 'UTF-8'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>connectionInfo</servlet-name>
<servlet-class>ic.ac.uk.icsoa.wladminserversensor.wsdlgen.Main_ptImpl</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>connectionInfo</servlet-name>
<url-pattern>/connectionInfo</url-pattern>
</servlet-mapping>
</web-app>
我的Java代碼:
package ic.ac.uk.icsoa.wladminserversensor.wsdlgen;
import ic.ac.uk.icsoa.wladminserversensor.WeblogicSensor;
import ic.ac.uk.icsoa.wladminserversensor.wsdlgentypes.ObjectFactory;
import ic.ac.uk.icsoa.wladminserversensor.wsdlgentypes.RequestSOAEndpointsMessageType;
import ic.ac.uk.icsoa.wladminserversensor.wsdlgentypes.ResponseSOAEndpointsMessageType;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.BindingType;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.soap.SOAPBinding;
@WebService(name = "main_pt", targetNamespace = "urn:WLAdminServerSensor", serviceName = "connectionInfoService", portName = "connectionInfo", wsdlLocation = "/WEB-INF/wsdl/connectionInfoService.wsdl")
@XmlSeeAlso({ ObjectFactory.class })
@javax.jws.soap.SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.DOCUMENT, parameterStyle = javax.jws.soap.SOAPBinding.ParameterStyle.BARE)
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public class Main_ptImpl {
@Resource
WebServiceContext ctxt;
public Main_ptImpl() {
}
@javax.jws.soap.SOAPBinding(parameterStyle = javax.jws.soap.SOAPBinding.ParameterStyle.BARE)
@Action(input = "urn:WLAdminServerSensor/querySOAEndpoints", output = "urn:WLAdminServerSensor/main_pt/querySOAEndpointsResponse")
@WebMethod(action = "urn:WLAdminServerSensor/querySOAEndpoints")
@WebResult(name = "responseSOAEndpointsMessage", targetNamespace = "http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types", partName = "requestSOAEndpointsOutputPart")
public ResponseSOAEndpointsMessageType querySOAEndpoints(@WebParam(name = "requestSOAEndpointsMessage", partName = "requestSOAEndpointsInputPart", targetNamespace = "http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types")
RequestSOAEndpointsMessageType requestSOAEndpointsInputPart) {
MessageContext mc = ctxt.getMessageContext();
HttpServletResponse response = (HttpServletResponse) mc.get(MessageContext.SERVLET_RESPONSE);
HttpServletRequest request = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
response.addHeader("Access-Control-Allow-Origin", "*");
if (request.getMethod().equals("OPTIONS")) {
response.addHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
response.addHeader("Access-Control-Allow-Methods","GET, POST, PUT");
return null;
};
return WeblogicSensor.execute(requestSOAEndpointsInputPart);
}
}
的Javascript運行在Chrome調用servlet(使用JQuery):
soapRequest = "<soap:Envelopexmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"xmlns:ic=\"http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types\"><soap:Header/><soap:Body><ic:requestSOAEndpointsMessage><ic:Input>CallviaJQuery</ic:Input><ic:Credentials><ic:Username>weblogic</ic:Username><ic:Password>*****</ic:Password></ic:Credentials></ic:requestSOAEndpointsMessage></soap:Body></soap:Envelope>";
var jqxhr = $.ajax({
type: 'POST',
url: param.endpoint,
data: {soapRequest},
dataType: 'xml',
contentType: 'application/soap+xml;charset=UTF-8;action="urn:WLAdminServerSensor/querySOAEndpoints"'
}).done(function(data) {
alert("TODO");
}).fail(function(data) {
alert("TODO - FAIL");
});