我已将My Jersey应用程序从1.x升级到2.15。我正在努力转换以前版本的所有注入绑定和其他自定义。如果我离开“JerseyApplication extends ResourseConfig”构造函数而没有任何定义,则基本设置正在运行。当我添加一个AbstractBinding时,一切都停止了。下面是设置:
的pom.xml
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!--<artifactId>jersey-container-servlet-core</artifactId>-->
<version>2.15</version>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.15</version>
</dependency>
的web.xml
<servlet>
<servlet-name>Jersey Spring Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.services.JerseyApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Spring Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
JerseyApplication.java
package com.services;
import org.glassfish.jersey.server.ResourceConfig;
public class JerseyApplication extends ResourceConfig {
public JerseyApplication(){
}
}
SimpleService.java
package com.services.shop;
import org.springframework.stereotype.Service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
@Path("/simple")
@Service
public class SimpleService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Foo> get() {
Foo foo1 = new Foo();
foo1.setName("Tester 1");
foo1.setBar("Apple");
foo1.setNumber(1234);
Foo foo2 = new Foo();
foo2.setName("Tester 2");
foo2.setBar("Green");
foo2.setNumber(5678);
List<Foo> results = new ArrayList<Foo>();
results.add(foo1);
return results;
}
}
Foo.java
package com.services.shop;
/**
* Created by pmefford on 2/12/15.
*/
public class Foo {
private String name;
private String bar;
private Integer number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
响应:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, DELETE, POST, OPTIONS
Access-Control-Allow-Credentials: true
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: application/json
Content-Length: 49
Date: Thu, 12 Feb 2015 18:31:39 GMT
[{"name":"Tester 1","bar":"Apple","number":1234}]
一旦我开始配置ResourceConfig,它就会失败: 更改了JerseyApplication.java
package com.services;
import org.glassfish.jersey.server.ResourceConfig;
public class JerseyApplication extends ResourceConfig {
public JerseyApplication(){
packages("com.services");
}
}
响应:
HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Transfer-Encoding: chunked
Date: Thu, 12 Feb 2015 18:34:10 GMT
Connection: close
<html><head><title>Apache Tomcat/7.0.57 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Servlet.init() for servlet Jersey Spring Web Application threw exception</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>Servlet.init() for servlet Jersey Spring Web Application threw exception</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>javax.servlet.ServletException: Servlet.init() for servlet Jersey Spring Web Application threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
</pre></p><p><b>root cause</b> <pre>java.lang.NoClassDefFoundError: com/sun/jersey/server/impl/inject/AbstractHttpContextInjectable
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:760)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:760)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:344)
org.glassfish.jersey.internal.util.ReflectionHelper$7.run(ReflectionHelper.java:402)
org.glassfish.jersey.internal.util.ReflectionHelper$7.run(ReflectionHelper.java:397)
java.security.AccessController.doPrivileged(Native Method)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener$AnnotatedClassVisitor.getClassForName(AnnotationAcceptingListener.java:262)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener$AnnotatedClassVisitor.visitEnd(AnnotationAcceptingListener.java:224)
jersey.repackaged.org.objectweb.asm.ClassReader.accept(ClassReader.java:697)
jersey.repackaged.org.objectweb.asm.ClassReader.accept(ClassReader.java:506)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.process(AnnotationAcceptingListener.java:170)
org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:892)
org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:849)
org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:755)
org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig._getClasses(ResourceConfig.java:1123)
org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:755)
org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1181)
org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1154)
org.glassfish.jersey.server.ResourceConfig.createRuntimeConfig(ResourceConfig.java:1150)
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:322)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:315)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
</pre></p><p><b>root cause</b> <pre>java.lang.ClassNotFoundException: com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:760)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:760)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:344)
org.glassfish.jersey.internal.util.ReflectionHelper$7.run(ReflectionHelper.java:402)
org.glassfish.jersey.internal.util.ReflectionHelper$7.run(ReflectionHelper.java:397)
java.security.AccessController.doPrivileged(Native Method)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener$AnnotatedClassVisitor.getClassForName(AnnotationAcceptingListener.java:262)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener$AnnotatedClassVisitor.visitEnd(AnnotationAcceptingListener.java:224)
jersey.repackaged.org.objectweb.asm.ClassReader.accept(ClassReader.java:697)
jersey.repackaged.org.objectweb.asm.ClassReader.accept(ClassReader.java:506)
org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.process(AnnotationAcceptingListener.java:170)
org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:892)
org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:849)
org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:755)
org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig._getClasses(ResourceConfig.java:1123)
org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:755)
org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1181)
org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1154)
org.glassfish.jersey.server.ResourceConfig.createRuntimeConfig(ResourceConfig.java:1150)
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:322)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:315)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
我需要能够配置它,因为我想按如下方式注册AbstractBinders:
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(LocaleInjectionFactory.class).to(Locale.class);
}
})
所以我的问题是为什么我不能在不丢失“默认”配置的情况下注册包或其他对象。我想将我的配置附加到那里,而不是明确定义所有内容。