2

我正在研究将 Jetty 嵌入 REST 服务的示例:

http://aredko.blogspot.com/2013/01/going-rest-embedding-jetty-with-spring.html

AppConfig 如下所示:

package com.example.config;

import java.util.Arrays;
import javax.ws.rs.ext.RuntimeDelegate;
import com.example.rs.HelloWorldService;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.rs.JaxRsApiApplication;

@Configuration
public class AppConfig {

    @Bean( destroyMethod = "shutdown" )
    public SpringBus cxf() {
        return new SpringBus();
    }

    @Bean
    public Server jaxRsServer() {
        JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );
        factory.setServiceBeans( Arrays.< Object >asList( helloWorldService() ) );
        factory.setAddress( "/" + factory.getAddress() );
        factory.setProviders( Arrays.< Object >asList( jsonProvider() ) );
        return factory.create();
    }

    @Bean
    public JaxRsApiApplication jaxRsApiApplication() {
        return new JaxRsApiApplication();
    }

    @Bean
    public HelloWorldService helloWorldService() {
        return new HelloWorldService();
    }

    @Bean
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }
}

我的 hello world 网络服务非常简单:

package com.example.rs;

import java.util.Collection;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path( "/hello" )
public class HelloWorldService {

    @Inject private JaxRsApiApplication jaxRsApiApplication;

    @Produces( { "text/plain" } )
    @GET
    public String helloWorld( ) {
        return "Hello, World";
    }
}

还有我的网络入门课程:

package com.example;

import org.apache.cxf.transport.servlet.CXFServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import com.example.config.AppConfig;

public class Starter {
    public static void main( final String[] args ) throws Exception {
        Server server = new Server( 8080 );

        // Register and map the dispatcher servlet
        final ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
        final ServletContextHandler context = new ServletContextHandler();
        context.setContextPath( "/" );
        context.addServlet( servletHolder, "/rest/*" );
        context.addEventListener( new ContextLoaderListener() );

        context.setInitParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );
        context.setInitParameter( "contextConfigLocation", AppConfig.class.getName() );

        server.setHandler( context );
        server.start();
        server.join();
    }
}

但是当我编译它并运行带有依赖项的生成 jar 时,我得到了多个堆栈跟踪,从这个开始:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxRsServer' defined in class com.example.config.AppConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.cxf.endpoint.Server com.example.config.AppConfig.jaxRsServer()] threw exception; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1029)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:925)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:771)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:763)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:706)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
at org.eclipse.jetty.server.Server.doStart(Server.java:277)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
at com.example.Starter.main(Starter.java:34)

我的目标是制作一个带有嵌入式 Jetty 的无 XML 独立 jar。我做错了什么,我该如何解决这个问题?

4

3 回答 3

0

我遇到了同样的问题......但是我认为这是特定机器上的问题......我在另外两台机器上检查了同一个项目并工作......我尝试了另一个服务器应用程序,但问题仍然存在...... .当我调试我的bean时,我意识到RuntimeDelegate通过我在那台机器上出现了一个异常......

于 2015-03-12T14:00:44.007 回答
0

你有 JaxRsApiApplication 类吗?

@ApplicationPath( "api" )
public class JaxRsApiApplication extends Application {
}
于 2014-03-01T12:55:19.823 回答
0

感谢您再次引起我的注意。这是一个工作版本:

https://github.com/mobiusinversion/rest-api-client-mvc/blob/master/src/main/java/com/example/application/web/SpringConfig.java

在我的情况下,解决方案是添加@DependsOn("cxf")jaxRsServerBean的声明中

@Bean
@DependsOn("cxf")
public Server jaxRsServer() {
    JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint(
            jaxRsApiApplication(), JAXRSServerFactoryBean.class );
    factory.setServiceBeans(
        Arrays.asList(
              healthCheck()
            , frontend()
            , rest()
        ));
    factory.setAddress("/" + factory.getAddress());
    factory.setProviders(Arrays.<Object>asList(jsonProvider()));
    return factory.create();
}

但自从我离开春天(谢天谢地:)搬到泽西岛。这也为嵌入式 Jetty 创建了一个不错的点火开关

public static void main(String[] args) throws Exception {

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    Server jettyServer = new Server(9090);
    jettyServer.setHandler(context);
    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "com.example.application");

    try {
        System.out.println("Starting Jetty");
        jettyServer.start();
        System.out.println("Jetty Started");
        jettyServer.join();
    } catch (Exception e) {
        System.out.println("Could not start server");
        e.printStackTrace();
    } finally {
        jettyServer.destroy();
    }
}

然后,您可以将 Jaxrs 路由放在组件扫描根目录下。

于 2015-03-13T01:52:09.557 回答