简体   繁体   English

找不到宁静的网络服务公共构造函数

[英]restful webservice public constructor not found

I am trying to invoke restful webservice using a client web application(simple client request from JSF MB).我正在尝试使用客户端 Web 应用程序(来自 JSF MB 的简单客户端请求)调用 Restful Web 服务。 I need to create an interface for the webservice and then invoke the method in it.我需要为 Web 服务创建一个接口,然后调用其中的方法。 But if i use @Path at interface, its giving an error - public constructor not found during Jboss deployment.但是如果我在接口上使用@Path,它会给出一个错误 - 在 Jboss 部署期间找不到公共构造函数。 But If i give give @Path at implementation class.但是如果我在实现类中给 @Path 。 It is invoking the service fine and is working properly.它正在很好地调用服务并且工作正常。 I need to have a Java interface between Rest client and Restful webservice impl classes.我需要在 Rest 客户端和 Restful webservice impl 类之间有一个 Java 接口。 Can anyone help?任何人都可以帮忙吗? can we have Rest interface or something?我们可以有 Rest 接口吗?

Client side:客户端:

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: 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

<?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>

Logs:日志:

[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]

I guess your mistake is that you give an interface to RestEasy and the framework is trying to create an instance of it via reflection.我猜你的错误是你为 RestEasy 提供了一个接口,而框架正试图通过反射创建它的一个实例。 Something like that will never work.这样的事情永远不会奏效。 Instead of the interface you should only specify implementation classes as a value for the parameter resteasy.resources .您应该只将实现类指定为参数resteasy.resources的值而不是接口。 If you want to to be more dynamically try a combination of OSGi and REST.如果您想更动态地尝试 OSGi 和 REST 的组合。 I didn't tried the example in this blog for myself, but you can have a look on that.我没有亲自尝试过这个博客中的例子,但你可以看看。

UPDATE更新

As you want to use your service from third party classes as well, why not wrap another interface with your REST webservice and that wrapped interface could be used in your third party classes.由于您还想从第三方类使用您的服务,为什么不将另一个接口与您的 REST web 服务包装在一起,并且该包装的接口可以在您的第三方类中使用。 The webservice is then only use to delegate requests to a specific implementation of this interface.然后,Web 服务仅用于将请求委托给此接口的特定实现。 You can inject the implementation via a Dependency Injection framework like Spring, Google Juice or something else.您可以通过依赖注入框架(如 Spring、Google Juice 或其他东西)注入实现。 Here is what I mean:这就是我的意思:

Your interface as an abstraction for your implementations:您的接口作为您的实现的抽象:

interface IExecutionService {
   void executeStuff();
}

Then your REST Service:然后你的 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();
   }
}

You can use the interface IExecutionService in combination with a specific DI framework in your third party interfaces as well.您也可以将接口IExecutionService与第三方接口中的特定 DI 框架结合使用。 Hope it's clear what I'm trying to do with the code above?!希望很清楚我想用上面的代码做什么?!

There is simple answer to the above problem.上面的问题有一个简单的答案。 In web.xml, instead of interface, i just had to configure IMPL class instead of interface and continue setting @Path at interface在 web.xml 中,而不是接口,我只需要配置 IMPL 类而不是接口并继续在接口设置 @Path

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

AND IT WORKED :)...它奏效了:)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM