简体   繁体   中英

Spring-boot jersey : resources not autodiscover

I try to use Spring-boot with jetty and jersey. No problem with the jetty part. I can start server and spring resources are running (trace, metrics,info,beans,....) but my resources didn't run.

My configuration files are : Launcher.java

@Configuration
@PropertySource("classpath:application.properties")
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.fdilogbox.report.serveur"})
public class Launcher extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Launcher.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Launcher.class);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/api/*");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, ResourcesConfiguration.class.getName());
        return registration;
    }

    @Bean
    public EmbeddedServletContainerFactory containerFactory() {
        final JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory() {
            @Override
            protected JettyEmbeddedServletContainer getJettyEmbeddedServletContainer(Server server) {
                return new JettyEmbeddedServletContainer(server);
            }
        };
        jettyEmbeddedServletContainerFactory.addServerCustomizers(new JettyConfiguration());
        return jettyEmbeddedServletContainerFactory;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

JettyConfiguration.java

public class JettyConfiguration implements JettyServerCustomizer {

    @Override
    public void customize(Server server) {
        WebAppContext webAppContext = (WebAppContext) server.getHandler();
        try {
            // Load configuration from resource file (standard Jetty xml configuration) and configure the context.
            createConfiguration("/jetty.xml").configure(webAppContext);
            createConfiguration("/jetty-rewrite.xml").configure(server);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private XmlConfiguration createConfiguration(String xml) throws IOException, SAXException {
        return new XmlConfiguration(Launcher.class.getResourceAsStream(xml));
    }
}

ResourcesConfiguration.java

public class ResourcesConfiguration extends ResourceConfig {

    public ResourcesConfiguration() {
        super();

        PackageNamesScanner resourceFinder = new PackageNamesScanner(new String[]{"com.fdilogbox.report.serveur.business.resources"}, true);
        registerFinder(resourceFinder);
        register(JacksonFeature.class);
    }
}

and my resources file :

@Path("builder")
@Component
public class ReportBuilderResource {

    @Autowired
    private ReportBuilderService reportBuilderService;

    @GET
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public String[] findAll() {
        return reportBuilderService.findAllReport();
    }
}

If I try to acces "localhost:9090/api/builder/list" I get an 404 error. But if I try "localhost:9090/bean" I get all bean on JSon format.

I think I have an error in my conf but I don't know where.

我发现了我的错误:管理端口是9090但普通资源端口是8090。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM