2016-05-16 45 views
0

我正在嘗試創建一個v2的web服務。爲此,我創建了一個新的包com.package.v2,其中聲明瞭遠程接口。如果我保持v2的類名稱與v1相同,那麼在部署到glassfish服務器期間出現錯誤。REST web服務的多個版本 - 執行錯誤

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 =線程2; |根資源類發現:

接口com.package .v2.TestRESTServiceBA

接口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類。