如何在同一端口托管SOAP服务和REST服务?
有谁知道如何配置它,以便SOAP服务和REST服务可以在应用程序服务器之外使用Jersey(Grizzly)共享相同的端口?如何在同一端口托管SOAP服务和REST服务?
- 我SOAP服务是在www.someurl.com:port/soap/crm
- 我休息服务是在www.someurl.com:port/crm
这些服务共享同端口但不是相同的基本URL,因此应该能够在该端口上并排运行。然而,有一个端口绑定错误..
所有这一切都在一个自定义的服务应用程序并没有web.xml文件或这样的。
的REST服务使用新泽西州和SOAP服务是在端点上发表的一类“ReceptionService”。 ':地址已在使用中:java.net.BindException绑定'
URI soapUri = URI.create("192.168.0.0:1234\soap\Crm")
URI restUri = URI.create("192.168.0.0:1234\crm")
// START SOAP SERVICE
Object reception = getObjectResource(ReceptionService.class);
Endpoint e = Endpoint.publish(soapUri, reception);
// START REST SERVICE
ResourceConfig rc = new ResourceConfig().packages("company.rest");
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(restUri, rc);
当我尝试这一点,灰熊吐出
我SOAP服务前台的设置是这样的:
@WebService(targetNamespace = "company.crm")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class Reception implements ReceptionService {
... methods
}
我休息服务类像往常一样注释...
@Path("hello/{username}")
public class Hello { ... }
我有点新手在这个nd任何指针,将不胜感激。请不要打扰我建议我运行应用程序服务器。这不是问题 - 问题是如何解决端口绑定问题而不移动到其他Web服务框架?
注RE BOUNTY:我不能ITERATE这还不够
“赏金优胜者将演示如何使用新泽西州的REST和Java SOAP服务(注释JAX-WS)一起在同一端点答案将不需要修改Rest和Soap注释类,但是,HTTP服务器代码更改或配置更改以使其工作是可接受的。切换到Jetty或其他应用程序服务器是不可接受的该解决方案必须100%嵌入并运行Java SE和Java Web服务库“。
如果您使用的是常见的容器如Tomcat为您web服务,那么你可以得到两个到达同一个端口上的服务请求。您可以将REST和基于SOAP的服务部署为应用程序的一部分。容器将接受传入请求并根据应用程序上下文将它们转发给应用程序。在您的应用程序web.xml中,您可以根据请求URL映射来配置发送请求的位置。
谢谢。我在客户服务器应用程序中使用Jersey(Glassfish以外)。我会在上面发布更多信息,也许有人有解决方案。 –
你所说的也不过是公开服务两种不同的接口,是的,你可以承载他们在一个端口上只在同一容器中部署它,你将有两个界面运行起来。
只要确保你没有上下文路径冲突,这似乎并没有在上面的问题中提到的网址发生。
例如让其余的接口被部署为:
www.someurl。com:port2/crm,所以不应该将soap部署在同一个url中,www.someurl.com:port1/soap/crm,这是正确的。
你还应该解释一下你是如何部署接口的,作为单独的war文件还是在单个war文件中。
谢谢。但我需要他们共享相同的URL端口。没有战争文件,没有应用程序服务器,只是编码如上。它可以在单独的端口上正常工作,但在共享相同端口时会导致绑定异常(尽管端点实际上是不同的)。 –
问题在于,Endpoint.publish将启动,HTTP Server和Grizzly部分也会启动HTTP因为这个服务器你有一个端口冲突。 –
那么我怎么让灰熊也提供肥皂服务? IE浏览器。并行发布接待服务? 必须有一种方法,因为Microsoft Server和Apache CMX也允许相同的行为。 –
无法在同一个端口上启动多个服务。
当您希望您的应用程序可通过同一端口访问时,您必须使用应用程序 服务器。
您必须注意,服务器上的两个应用都可以使用不同的URL(web.xml)访问。
现在可以通过相同的端口访问同一应用程序服务器上的所有应用程序。
如果可以使用应用程序服务器完成,则可以在没有编程的情况下完成。那么Glassfish如何在同一个端口上使用球衣(grizzly http)作为其余服务器和肥皂服务......这是百万美元的问题:) –
Jetty的覆盖功能允许您在同一个Jetty实例/端口上部署两个不同路径的不同路径。
您将拥有一个带有Jersey servlet的web.xml(适用于REST),另一个带有SOAP servlet的web.xml。
http://www.eclipse.org/jetty/documentation/current/overlay-deployer.html
好的。如果我将这一切都转移到Jetty,这可能会奏效。由于我现在使用Jetty来处理Restful事物,因为我喜欢它与Guice轻松配置的方式,所以也许Soap也可以这样。 –
但是,它必须是一个不涉及web.xml文件的解决方案。如果你可以展示如何用Jetty和没有xml文件来做到这一点,那么我会考虑一个正确的答案。 –
这是大多数代码我使用单个端口(2个不同的上下文路径),(使用灰熊明显)完全嵌入在我的应用程序上同时承载休息和肥皂服务,以及弹簧,其构造的。 ..
package com.mycompany.structure.web.grizzly;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.BindException;
import java.util.EnumSet;
import java.util.LinkedList;
import javax.servlet.DispatcherType;
import javax.servlet.Servlet;
import com.mycompany.structure.web.jersey.jackson.JsonResourceConfig;
import com.mycompany.structure.web.jersey.spring.ExposedApplicationContext;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;
import org.glassfish.grizzly.jaxws.JaxwsHandler;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.servlet.ServletContainer;
import org.smallmind.nutsnbolts.lang.web.PerApplicationContextFilter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
public class GrizzlyInitializingBean implements DisposableBean, ApplicationContextAware, ApplicationListener, BeanPostProcessor {
private static final Class[] NO_ARG_SIGNATURE = new Class[0];
private HttpServer httpServer;
private LinkedList<WebService> serviceList = new LinkedList<>();
private ServletInstaller[] servletInstallers;
private String host;
private String contextPath;
private String restPath;
private String soapPath;
private int port;
private boolean debug = false;
public void setHost (String host) {
this.host = host;
}
public void setPort (int port) {
this.port = port;
}
public void setContextPath (String contextPath) {
this.contextPath = contextPath;
}
public void setRestPath (String restPath) {
this.restPath = restPath;
}
public void setSoapPath (String soapPath) {
this.soapPath = soapPath;
}
public void setServletInstallers (ServletInstaller[] servletInstallers) {
this.servletInstallers = servletInstallers;
}
public void setDebug (boolean debug) {
this.debug = debug;
}
@Override
public synchronized void onApplicationEvent (ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
if (debug) {
System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true");
}
httpServer = new HttpServer();
httpServer.addListener(new NetworkListener("grizzly2", host, port));
WebappContext webappContext = new WebappContext("Grizzly Application Context");
webappContext.addServlet("JAX-RS Application", new ServletContainer(new JsonResourceConfig(ExposedApplicationContext.getApplicationContext()))).addMapping(restPath + "/*");
webappContext.addFilter("per-application-data", new PerApplicationContextFilter()).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), restPath + "/*");
webappContext.addListener("org.springframework.web.context.request.RequestContextListener");
for (ServletInstaller servletInstaller : servletInstallers) {
try {
Constructor<? extends Servlet> servletConstructor;
Servlet servlet;
String urlPattern;
servletConstructor = servletInstaller.getServletClass().getConstructor(NO_ARG_SIGNATURE);
servlet = servletConstructor.newInstance();
webappContext.addServlet(servletInstaller.getDisplayName(), servlet).addMapping((urlPattern = servletInstaller.getUrlPattern()) == null ? "/" : urlPattern);
}
catch (Exception exception) {
throw new GrizzlyInitializationException(exception);
}
}
webappContext.deploy(httpServer);
for (WebService webService : serviceList) {
HttpHandler httpHandler = new JaxwsHandler(webService.getService(), false);
httpServer.getServerConfiguration().addHttpHandler(httpHandler, soapPath + webService.getPath());
}
try {
httpServer.start();
}
catch (IOException ioException) {
if (!(ioException instanceof BindException)) {
throw new GrizzlyInitializationException(ioException);
}
}
}
}
@Override
public void setApplicationContext (ApplicationContext applicationContext) {
ExposedApplicationContext.register(applicationContext);
}
@Override
public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {
ServicePath servicePath;
if ((servicePath = bean.getClass().getAnnotation(ServicePath.class)) != null) {
serviceList.add(new WebService(servicePath.value(), bean));
}
return bean;
}
@Override
public synchronized void destroy() {
if (httpServer != null) {
httpServer.shutdown();
}
}
}
太棒了。我会看看它。 –
有点更多的信息会有帮助,例如你使用哪种类型的Web服务器/应用程序服务器? 'web.xml'看起来像什么? –
您是否找到了解决方案?我面临同样的问题 –
没有。我仍然让他们分开的端口。我将不得不重做我的代码,我没有时间和耐心。希望将来可以配置。 –