找到你要的答案

Q:Spring-boot - how to use both Resteasy JAX-RS and Spring MVC controllers

Q:春引导如何使用RESTEasy JAX-RS和Spring MVC控制器

I have a jax-rs resources which I am trying to convert to spring boot. I have made them work but looks like some of jax-rs resources which are included in project as dependencies from other projects. I know it should be a service! Anyway so I may be able to make change to that library and make them mvc compatible but is there another way I can make spring servlet work with both? I am using all java based configuration btw no xml.

Main application class is currently annotated as following unless I add "/" in url-mapping which moots the point of adding this. I don't even see why I should add this if dispatcher servlet can find handler for every / request.

@SpringBootApplication(exclude = { EmbeddedServletContainerFactory.class })
@EnableAsync
@ImportResource(value = { "classpath:springmvc-resteasy.xml" })
public class Application implements AsyncConfigurer, DisposableBean { .. }

I think with above spring DispatcherServlet is still at front in control and it knows of rest easy handleradapters. By default boot also maps everything under '/' so I dont understand why it doesnt work. I am getting 404 when hitting jax-rs uri, spring mvc uri works.

I tried to add following but after that everything stopped working.

@Bean
    public ServletRegistrationBean initServlet(DispatcherServlet dispatcherServlet) {

        ServletRegistrationBean servletRegistrationBean 
        = new ServletRegistrationBean(dispatcherServlet, false,"/jaxrs-api/*", "/mvc-api/*");

        servletRegistrationBean
    .setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);

        return servletRegistrationBean;
    }

I can see following beans are being loaded for resteasy upon context initialization.

resteasy.deployment
resteasy.registry
resteasy.dispatcher
resteasy.providerFactory
resteasy.spring.bean.processor
abstract.resteasy.handlerMapping
resteasy.handlerMapping
resteasy.handlerAdapter
resteasy.exception.handler
resteasy.no.resource.found.view
reateasy.error.view.resolver

我有一个JAX RS资源,我试图把春季启动。我已经让他们工作但看起来一些Jax RS资源中包含的项目与其他项目的依赖关系。我知道这应该是一种服务!所以我可以换到图书馆,让他们MVC兼容但有另一种方式我都可以使弹簧Servlet的工作吗?我用所有的基于java的配置还有没有XML。

目前主要应用类注释如下除非我添加“/”的URL映射中添加这一点。我甚至不明白我为什么要加入这个如果调度员Servlet可以找到所有/请求处理程序。

@SpringBootApplication(exclude = { EmbeddedServletContainerFactory.class })
@EnableAsync
@ImportResource(value = { "classpath:springmvc-resteasy.xml" })
public class Application implements AsyncConfigurer, DisposableBean { .. }

我认为上述的Spring DispatcherServlet仍在前面控制,它知道放心handleradapters。默认情况下,开机还映射一切在' / ',所以我不明白为什么它不工作。我要404打jax RS URI时,Spring MVC的URI的作品。

我试图添加以下,但之后一切停止工作。

@Bean
    public ServletRegistrationBean initServlet(DispatcherServlet dispatcherServlet) {

        ServletRegistrationBean servletRegistrationBean 
        = new ServletRegistrationBean(dispatcherServlet, false,"/jaxrs-api/*", "/mvc-api/*");

        servletRegistrationBean
    .setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);

        return servletRegistrationBean;
    }

我可以看到下面的豆子是RESTEasy在上下文初始化加载。

resteasy.deployment
resteasy.registry
resteasy.dispatcher
resteasy.providerFactory
resteasy.spring.bean.processor
abstract.resteasy.handlerMapping
resteasy.handlerMapping
resteasy.handlerAdapter
resteasy.exception.handler
resteasy.no.resource.found.view
reateasy.error.view.resolver
answer1: 回答1:

It looks like things started working again after some try and errors which is not the way this should be! Spring-boot really should document here how it can work with spring mvc and jax-rs in co-existence.

Here's the sequence of changes I did.

I added @EnableWebMvc to my Application.java class or It can be on any other @Configuration class

@SpringBootApplication(exclude = { EmbeddedServletContainerFactory.class })
@EnableAsync
@EnableWebMvc
@ImportResource(value = { "classpath:springmvc-resteasy.xml" })
public class Application implements AsyncConfigurer, DisposableBean { .. }

In hindsight I knew this gonna create some problem and it did! My additional MessageConverter stopped working. I had following in Application.java

@Bean
  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper scalaObjectMapper = new ScalaObjectMapper();
    //scalaObjectMapper.enable(features)
    //objectMapper.registerModule(new DefaultScalaModule());
    scalaObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jsonConverter.setObjectMapper(scalaObjectMapper);
    return jsonConverter;
  }

Now I had to move it to another config class which overrides WebMvcConfigurerAdapter to customize spring mvc beans.

@Component
@Configuration
public class WebMvcCustomConfig extends WebMvcConfigurerAdapter {

  /**
   * Registering Scala ObjectMapper
   * @return
   */
  @Bean
  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper scalaObjectMapper = new ScalaObjectMapper();
 scalaObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jsonConverter.setObjectMapper(scalaObjectMapper);
    return jsonConverter;
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    //converters.add(ScalaObjectMapper);
    super.configureMessageConverters(converters);
    converters.add(customJackson2HttpMessageConverter());
  }
}

So looks like now Spring MVC has some control rather then Spring Boot. I am sure this is gonna break loose when I have some additional components for example swagger2 integration or something similar.

看起来事情开始工作后再次尝试和错误,这不是这样的应该!春天真的应该启动文件如何与共存的Spring MVC和jax RS工作。

这是我做的改变的顺序。

我说“enablewebmvc我的java类也可以是任何其他“配置类

@SpringBootApplication(exclude = { EmbeddedServletContainerFactory.class })
@EnableAsync
@EnableWebMvc
@ImportResource(value = { "classpath:springmvc-resteasy.xml" })
public class Application implements AsyncConfigurer, DisposableBean { .. }

事后我知道这会造成一些问题!我的额外的MessageConverter停止工作。我有以下的java

@Bean
  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper scalaObjectMapper = new ScalaObjectMapper();
    //scalaObjectMapper.enable(features)
    //objectMapper.registerModule(new DefaultScalaModule());
    scalaObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jsonConverter.setObjectMapper(scalaObjectMapper);
    return jsonConverter;
  }

现在我不得不将它移动到另一个配置类,重写webmvcconfigureradapter定制Spring MVC豆。

@Component
@Configuration
public class WebMvcCustomConfig extends WebMvcConfigurerAdapter {

  /**
   * Registering Scala ObjectMapper
   * @return
   */
  @Bean
  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper scalaObjectMapper = new ScalaObjectMapper();
 scalaObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jsonConverter.setObjectMapper(scalaObjectMapper);
    return jsonConverter;
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    //converters.add(ScalaObjectMapper);
    super.configureMessageConverters(converters);
    converters.add(customJackson2HttpMessageConverter());
  }
}

所以现在看起来像Spring MVC有一些控制而不是春天启动。我相信这是要挣脱时,我有一些额外的组件,例如swagger2整合或类似的东西。

java  spring  spring-mvc  tomcat  spring-boot