我正在尝试创建一个Web服务的v2。为此,我创建了一个新的包com.package.v2,其中声明了远程接口。如果我将v2的类名保持为与v1相同,那么在部署到glassfish服务器期间我会收到错误。
SEVERE|glassfish3.1.2|com.sun.jersey.spi.inject.Errors|_ThreadID=61;_ThreadName=Thread-2;|The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: The class com.package.v2.TestRESTServiceBA is an interface and cannot be instantiated.|#]
如果我将v2类重命名为其他名称,那么一切正常。你能告诉我怎样才能保留同名的版本吗?
更多细节: INFO | glassfish3.1.2 | com.sun.jersey.api.core.ScanningResourceConfig | _ThreadID = 61; _ThreadName = Thread-2; |找到根资源类:
interface com.package.v2.TestRESTServiceBA
interface com.package.v1.TestRESTServiceBA
添加代码:
版本1接口
package com.package.v1;
import java.util.Map;
import javax.ejb.Remote;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.package.common.dto.ResponseDTO;
@Remote
@Path("/v1/test")
public interface TestRESTServiceBA {
@GET
@Path("/headers")
@Produces("application/json")
ResponseDTO<Map<String, String>> getAllHeaders();
}
版本1实施
package com.package.v1;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.package.common.dto.ResponseDTO;
import com.package.v1.TestRESTServiceBA;
@Stateless
public class TestRESTServiceBABean implements TestRESTServiceBA {
private static final Log LOG = LogFactory.getLog(TestRESTServiceBABean.class);
@Context
private HttpServletRequest request;
public TestRESTServiceBABean() {
}
@Override
public ResponseDTO<Map<String, String>> getAllHeaders() {
LOG.info("GET:v1/test/headers requested!");
final ResponseDTO<Map<String, String>> response = new ResponseDTO<>();
return response;
}
}
版本2界面 包com.package.v2;
import java.util.Map;
import javax.ejb.Remote;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.package.common.dto.ResponseDTO;
@Remote
@Path("/v2/test")
public interface TestRESTServiceBA2 {
@GET
@Path("/headers")
@Produces("application/json")
ResponseDTO<Map<String, String>> getAllHeaders();
}
第2版实施 包com.package.v2;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.package.common.dto.ResponseDTO;
import com.package.common.model.DealerSpeedUser;
import com.package.v2.TestRESTServiceBA2;
@Stateless
public class TestRESTServiceBABean implements TestRESTServiceBA {
private static final Log LOG = LogFactory.getLog(TestRESTServiceBABean2.class);
@Context
private HttpServletRequest request;
public TestRESTServiceBABean2() {
}
@Override
public ResponseDTO<Map<String, String>> getAllHeaders() {
LOG.info("GET:v2/test/headers requested!");
final ResponseDTO<Map<String, String>> response = new ResponseDTO<>();
return response;
}
}
答案 0 :(得分:0)
错误告诉您它无法创建接口实例,因为您已在接口类上添加了@Path
@GET
,@Produces
等注释。将这些注释移到TestRESTServiceBABean类中。