RestLet:写入响应实体时发生异常

时间:2015-04-20 19:50:18

标签: java rest restlet restful-architecture

我刚刚开始创建我的第一个restlet应用程序。我已经按照restlet第一步教程。我已经完全按照规定创建了所有文件,当我运行它时服务器运行得很好但是当我通过邮递员调用我的服务时我得到了这个错误:

错误:

2015-04-20  20:39:53    0:0:0:0:0:0:0:1 -   -   8182    GET /firstSteps/hello   -   200 12  0   156 http://localhost:8182   Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36    -
An exception occured writing the response entity

java.lang.ClassCastException: org.restlet.data.Header cannot be cast to org.restlet.engine.header.Header
    at org.restlet.ext.simple.internal.SimpleCall.writeResponseHead(SimpleCall.java:304)
    at org.restlet.engine.adapter.ServerCall.sendResponse(ServerCall.java:450)
    at org.restlet.engine.adapter.ServerAdapter.commit(ServerAdapter.java:187)
    at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:144)
    at org.restlet.ext.simple.internal.SimpleContainer.handle(SimpleContainer.java:80)
    at org.simpleframework.http.core.Dispatcher.dispatch(Dispatcher.java:121)
    at org.simpleframework.http.core.Dispatcher.run(Dispatcher.java:103)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Error while handling an HTTP server call

这是我的代码

Helloworld Class:

package smartRoom;


import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
 * Resource which has only one representation.
 */
public class Helloworld extends ServerResource {


    @Get
    public String represent() {

        return "hello, world";
    }

}

这是我的应用程序类:

package smartRoom;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;

public class HelloworldAppilcation extends Application {


    public static void main(String[] args) throws Exception {  
        // Create a new Component.  
        Component component = new Component();  

        // Add a new HTTP server listening on port 8182.  
        component.getServers().add(Protocol.HTTP, 8182);  

        // Attach the sample application.  
        component.getDefaultHost().attach("/firstSteps",  
                new HelloworldAppilcation());  

        // Start the component.  
        component.start();  
    }    



    /**
     * Creates a root Restlet that will receive all incoming calls.
     */
    @Override
    public synchronized Restlet createInboundRoot() {
        // Create a router Restlet that routes each call to a new instance of HelloWorldResource.
        Router router = new Router(getContext());

        // Defines only one route
        router.attach("/hello", Helloworld.class);

        return router;
    }

}

我无法在网上找到有关此错误的任何内容。我按照指令开始重新创建项目,但仍然遇到同样的错误。我不确定我做错了什么,任何帮助都会得到很大的帮助。

更新

是的我可以确认端口是开放的并且正在聆听

enter image description here

更新两个

http://restlet.com/technical-resources/restlet-framework/guide/2.2/editions/jse/overview

我现在也尝试了maven教程并获得了相同的结果。

更新三

我今天再给它一次,从头开始重新下载所有内容,这次它完美运行。不确定问题是什么,但最有可能与restlet的版本有关。

2 个答案:

答案 0 :(得分:1)

我建议你使用Restlet版本2.3和Jetty这样的服务器连接器(在类路径中添加扩展名org.restlet.ext.jetty)。

据我所知,您使用Restlet实现了一个正确的启动应用程序。

您能告诉我们您使用的Restlet的确切版本吗?

以下是您可以针对您的应用尝试的文件pom.xml的示例:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.restlet</groupId>
    <artifactId>restlet-starter</artifactId>
    <name>${project.artifactId}</name>
    <packaging>jar</packaging>
    <version>1.0.0-snapshot</version>

    <properties>
        <restlet-version>2.3.1</restlet-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.jetty</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
</project>

希望它可以帮到你, 亨利

答案 1 :(得分:0)

对我来说,按照建议将扩展名org.restlet.ext.jetty添加到类路径后它才起作用。顺便说一下,我用Gradle来建立项目。