我正在尝试将Jersey 2.22.2与Jsp和Spring集成。我正面临一些困难使它发挥作用。我正在使用Servlet 3.0风格的Java配置。我附上了代码。
@Path("spider")
@Template
@Component
@Data
@Slf4j
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class DashboardController {
@Autowired
private SpiderWeb dashboard;
private Map<String, Spider> spiderMap = new HashMap<String, Spider>();
@GET
@Path("/jsp/dashboard")
@Produces(MediaType.TEXT_HTML)
public Response process(@QueryParam("name") final String name,
@QueryParam("timeLapse") final Long timeLapse) {
if(spiderMap.isEmpty()) {
for (Spider spider : dashboard.getSpiders()) {
spiderMap.put(spider.getName(), spider);
}
}
if (name != null && timeLapse != null && spiderMap.containsKey(name)) {
log.info("re-index '" + name + "'");
spiderMap.get(name).trigger(timeLapse);
}
return Response.ok().entity(new Viewable("/dashboard", dashboard)).build();
}
}
Java配置类,用于创建WebappContext并注册JspMvcFeature类和提供程序包。
package com.sky.search.spider.api;
import com.xxx.search.stack.http.StackApplication;
import com.xxx.search.stack.http.StackHttpServer;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.servlet.ServletContainer;
import org.springframework.web.context.ContextLoaderListener;
public class SpiderStackHttpServer extends StackHttpServer{
public SpiderStackHttpServer(StackApplication application, boolean enableJmx) {
super(application, enableJmx);
}
@Override
public void deployServletContext(HttpServer server) {
WebappContext context = new WebappContext("WebappContext", "");
context.setInitParameter("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
ServletRegistration registration = context.addServlet("ServletContainer" , ServletContainer.class);
registration.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "com.sky.search.spider.web");
registration.setLoadOnStartup(1);
registration.setInitParameter("jersey.config.server.mvc.templateBasePath.jsp","/WEB-INF/jsp");
registration.setInitParameter("contextClass" , "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
//registration.setInitParameter("contextConfigLocation" , "com.sky.search.spider.spring.ApiConfig");
registration.setInitParameter("jersey.config.server.provider.classnames", "org.glassfish.jersey.server.mvc.jsp.JspMvcFeature");
registration.addMapping("/jsp/dashboard/*");
context.addListener(ContextLoaderListener.class);
context.deploy(server);
}
}
我们正在使用自己的Custom Java类来启动HttpServer
public StackHttpServer(StackApplication application, boolean enableJmx) {
GrizzlyHttpContainer container = new GrizzlyHttpContainerProvider().createContainer(GrizzlyHttpContainer.class, application.getConfiguration());
this.server = createHttpServer(application.getUri(), container, false, null, false);
this.server.getServerConfiguration().setJmxEnabled(enableJmx);
configureThreads(server);
try {
server.start();
deployServletContext(server);
} catch (final IOException ex) {
server.shutdownNow();
throw new ProcessingException(LocalizationMessages.FAILED_TO_START_SERVER(ex.getMessage()), ex);
}
}
因此,当我启动服务器时,我收到以下错误。
2016-02-23 18:38:05,186错误 [org.glassfish.jersey.message.internal.WriterInterceptorExecutor] - 找不到媒体类型= text / html,type = class的MessageBodyWriter org.glassfish.jersey.server.mvc.Viewable,genericType = class org.glassfish.jersey.server.mvc.Viewable。
我在Jersey网站上看到Servlet 3.0风格不起作用。 https://jersey.java.net/documentation/latest/user-guide.html#d0e15405 但我认为它可能已经过时,我会在这方面请求你的帮助。