java jersey.server.model.ModelValidationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36700816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:47:22  来源:igfitidea点击:

jersey.server.model.ModelValidationException

javarestjersey

提问by Maks.Burkov

Hello I have some problem with this code! I got this error: Please Help!

您好,这段代码有问题!我收到此错误:请帮助!

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.lang.String jersey.JerseyTesting.getName() and public java.lang.String jersey.JerseyTesting.getPassword() at matching regular expression /jerseytesting. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@4d73a7a'] org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555) org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184) org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350) org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347) org.glassfish.jersey.internal.Errors.process(Errors.java:315) org.glassfish.jersey.internal.Errors.process(Errors.java:297) org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255) org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:347) org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:392) org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177) org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Unknown Source)

org.glassfish.jersey.server.model.ModelValidationException:应用程序初始化期间应用程序资源模型的验证失败。[[致命] 资源模型具有用于 HTTP 方法 GET 和输入 MIME 类型的模糊(子)资源方法,如 Java 方法 public java.lang.String jersey.JerseyTesting 中的“@Consumes”和“@Produces”注释所定义。 getName() 和 public java.lang.String jersey.JerseyTesting.getPassword() 匹配正则表达式 /jerseytesting。这两种方法产生和使用完全相同的 MIME 类型,因此它们作为资源方法的调用总是会失败。source='org.glassfish.jersey.server.model.RuntimeResource@4d73a7a'] org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555) org.glassfish.jersey.server。

Mine Class:
@Path("/JerseyTesting")
public class JerseyTesting {
String name = "Maks";
String password = "pl000pl";
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getName() {
    return name;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getPassword() {
    return password;
}
}


Mine web.xml page!
<display-name>JerseyTesting</display-name>  
    <servlet>   
    <servlet-name>JerseyTesting</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>jersey</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>JerseyTesting</servlet-name>
    <url-pattern>/RestTesting</url-pattern> 
    </servlet-mapping>

回答by Paul Samsotha

[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.lang.String jersey.JerseyTesting.getName() and public java.lang.String jersey.JerseyTesting.getPassword() at matching regular expression /jerseytesting. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.;

[[致命] 资源模型具有用于 HTTP 方法 GET 和输入 MIME 类型的模糊(子)资源方法,如 Java 方法 public java.lang.String jersey.JerseyTesting 中的“@Consumes”和“@Produces”注释所定义。 getName() 和 public java.lang.String jersey.JerseyTesting.getPassword() 匹配正则表达式 /jerseytesting。这两种方法产生和使用完全相同的 MIME 类型,因此它们作为资源方法的调用总是会失败。

Your resource methods are ambiguous

你的资源方法不明确

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getName() {
    return name;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getPassword() {
    return password;
}

Jersey will not know which one to pick. You will need to either change the path on one or both of them or the media type. Most likely you will want to change the path for this particular case. Something like

泽西岛不知道该选哪一个。您需要更改其中一个或两个的路径或媒体类型。您很可能希望更改此特定情况的路径。就像是

@GET
@Path("/name")
@Produces(MediaType.TEXT_PLAIN)
public String getName() {
    return name;
}
@GET
@Path("/password")
@Produces(MediaType.TEXT_PLAIN)
public String getPassword() {
    return password;
}