2013-02-21 67 views
2

我想调用restful webservice使用客户端web应用程序(简单的客户端请求从JSF MB)。我需要为webservice创建一个接口,然后调用其中的方法。但是,如果我在接口使用@Path,它会给出一个错误 - 在Jboss部署期间找不到公共构造函数。但是如果我给实施课的@Path。它正在调用服务并且工作正常。我需要在Rest客户端和Restful webservice impl类之间有一个Java接口。谁能帮忙?我们可以有Rest接口吗?宁静的webservice公共构造函数未找到

客户端:

ClientRequest request = new ClientRequest("localhost:8080/RESTfulExample/json/...../"); 
ClientResponse<String> response = request.get(String.class); 

RestInterface:

package com.nee.interfaces; 
import javax.ws.rs.Path; 
import javax.ws.rs.GET; 

@Path("/OrderManagementService") 
public interface OrderManagementServiceInterface { 
    @GET 
    @Path("/GetListOfOrders") 
    void getListOfOrders(); 
} 

IMPL:

package com.nee.implementations; 
import com.nee.interfaces.OrderManagementServiceInterface; 


public class OrderManagementServiceImpl implements OrderManagementServiceInterface { 
    public void getListOfOrders() { 
     System.out.println("am here"); 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<context-param> 
    <param-name>resteasy.resources</param-name> 
    <param-value> 
     com.nee.interfaces.OrderManagementServiceInterface 
    </param-value> 
</context-param> 

<!-- this has to match with resteasy-servlet url-pattern --> 

<context-param> 
    <param-name>resteasy.servlet.mapping.prefix</param-name> 
    <param-value>/rest</param-value> 
</context-param> 

<!-- to return data according to extension --> 

<context-param> 
    <param-name>resteasy.media.type.mappings</param-name> 
    <param-value>json : application/json, xml : application/xml</param-value> 
</context-param> 

<listener> 
    <listener-class> 
     org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap 
    </listener-class> 
</listener> 

<servlet> 
    <servlet-name>resteasy-servlet</servlet-name> 
    <servlet-class> 
     org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher 
    </servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>resteasy-servlet</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 
</web-app> 

日志:

[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/web]] 
(MSC service thread 1-2) Exception sending context initialized event to listener instance of class 
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap: java.lang.RuntimeException: Unable to find a public constructor for class com.seagate.interfaces.OrderManagementServiceInterface 
at org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory.registered(POJOResourceFactory.java:35) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:120) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:106) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:83) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:72) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:383) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:225) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap.contextInitialized(ResteasyBootstrap.java:28) [resteasy-jaxrs-2.3.3.Final-redhat-1.jar:2.3.3.Final-redhat-1] 
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.16.Final-redhat-1.jar:] 
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.16.Final-redhat-1.jar:] 
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1] 
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) 
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)[rt.jar:1.7.0_09] 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_09] 
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_09] 
+0

发布您的代码。如果您使用的是接口,则应该在其上定义所有JAX-RS注释。 – Perception 2013-02-21 01:05:13

+0

请用格式良好的代码更新您的文章。 – jdevelop 2013-02-21 01:19:02

+0

您的界面代码不可编译。我们也需要你的实现代码。 – Perception 2013-02-21 01:29:45

回答

1

对上述问题有一个简单的答案。在web.xml中,而不是接口,我不得不配置IMPL类,而不是接口,并继续在接口

<context-param> 
    <param-name>resteasy.resources</param-name> 
    <param-value> 
     com.seagate.implementations.OrderManagementServiceImpl 
    </param-value> 
</context-param> 

设置@Path和它的工作:)...

+0

这就是我在我的答案中对你说的!第一部分 – 2013-03-06 14:40:49

1

我猜你的错误是你给的接口到RestEasy的和框架试图通过反射来创建它的一个实例。像这样的东西永远不会工作。您应该只将实现类指定为参数resteasy.resources的值,而不是接口。如果你想更动态地尝试OSGi和REST的组合。我自己并没有在this的博客中尝试过这个例子,但你可以看看这个例子。

UPDATE

当你想使用第三方类服务,以及,为什么不换另一个接口与REST Web服务和包裹接口可以在您的第三方类使用。然后,web服务仅用于将请求委托给此接口的特定实现。您可以通过Spring,Google Juice或其他的依赖注入框架注入实现。这里是我的意思是:

你的界面,为您实现一个抽象:

interface IExecutionService { 
    void executeStuff(); 
} 

那么你的REST服务:

@Path("/OrderManagementService") 
class ExectionServiceDelegator { 

    private IExecutionService wrappedServiceInterface; 

    /** 
    * play around with some DI frameworks for injecting your implementation 
    */ 
    public void setIExecutionService(IExecutionService service) { 
     wrappedServiceInterface = service;  
    } 

    @GET 
    @Path("/GetListOfOrders") 
    public void doingSomething() { 
     service.executeStuff(); 
    } 
} 

您可以使用该接口IExecutionService结合特定的DI你的第三方接口也是如此。希望很清楚我想要用上面的代码做什么?

+0

感谢christian ...所以我们无法使用界面来获得没有OSGI的resteasy ??? ..这真的很伤心。通常对于基于SOAP的Web服务,我们曾经有一个接口来调用服务。 – user2093576 2013-02-21 06:10:06

+0

即使在OSGi中,您通常不会使用直接接口来抽象您的REST服务。我认为这并没有太大意义。为什么?因为在从客户端调用资源时实际上不适用于特定的实现。你只需调用一个URL而不需要任何类。只有您的应用程序服务器负责将这些URL映射到资源(在您的示例中是您在类中指定的REST资源路径)。这意味着URL是某种时尚的“界面”。您只需为类定义相同的路径即可轻松更改实现... – 2013-02-21 07:20:51

+0

...只需更改'resteasy.resources'参数即可将URL路径映射到您的新类。希望现在更清楚一点,您不必深深依赖Java接口:)正如您所看到的,所提供的博客中的示例并不使用接口。 ;) – 2013-02-21 07:22:13

相关问题