首页 > 解决方案 > 在 Restlet 中发布一个简单的 JsonRepresentation

问题描述

嗨,我是 RestLet 的新手,这是一个简单的示例,我想发布一个 Json 表示,但是在运行客户端后我遇到了下面提到的错误。请在这方面帮助我。非常感谢。

资源:

 import org.json.JSONException;
    import org.json.JSONObject;
    import org.restlet.ext.json.JsonRepresentation;
    import org.restlet.resource.Post;
    import org.restlet.resource.ServerResource;
    import org.restlet.data.Status;

    public class DepResource extends ServerResource{
        String jsonString="";


        @Post
        public void acceptJsonRepresentation(JsonRepresentation entity) {

            JSONObject json = null;

            try {
                json = entity.getJsonObject();
                // business logic and persistence
                String jsonPost=json.toString();
                System.out.println(jsonPost);

            } catch (JSONException e) {

              setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                return;
            } 

        }


    }

客户端在第 44 行出现错误

import java.io.IOException;
import java.net.InetAddress;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.CharacterSet;
import org.restlet.data.Method;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.ClientResource;

public class Client {

    public static void main(String[] args) throws JSONException, IOException {


        /**##POST Prepration##**/

        JSONObject jsonObjectGraph = new JSONObject(); 
        jsonObjectGraph.put("Traffic", 100);
        jsonObjectGraph.put("Disksize", 20);
        String str=jsonObjectGraph.toString();


        JsonRepresentation JRRepDep = new JsonRepresentation(str);      
        JRRepDep.setCharacterSet(CharacterSet.UTF_8);
        System.out.println("with jsonrepresentation: "+JRRepDep.getText());

        // TODO Auto-generated method stub
        /**********POST**************/
        String baseURL1 = "http://" + InetAddress.getLocalHost().getHostAddress() + ":" + "8181";
        // Specifying the URL for the resource
        String resourceName = "/files";

        String ApplicationServerName = baseURL1 + resourceName;
        System.out.println("URI at client: " +ApplicationServerName);

        // Specifying the REST client and post to REST server

        ClientResource restletClient = new ClientResource(ApplicationServerName);
        System.out.println(ApplicationServerName);

        restletClient.setMethod(Method.POST);
        System.out.println("dovomi");
        restletClient.post(JRRepDep);
        System.out.println("After post");



        // Checking the status of the post request
        if (restletClient.getStatus().isSuccess()) 
        {
            System.out.println("POST Request success.");
            restletClient.release();
        }
    }

}

服务器正在运行,没有任何错误

import java.net.InetAddress;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;



public class DepServer  extends Application {
    private static String ipAddress;
    private static int port;

    public static String getURI()
    {
        return  "http://" + ipAddress + ":" + port;
    }


    public static void main(String[] args) {

        try {
            ipAddress = InetAddress.getLocalHost().getHostAddress();
            port = 8181;

            Server server = new Server(Protocol.HTTP, ipAddress, port);
            server.setNext(new DepServer());
            server.start();
            System.out.print("Server is running");



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Restlet createInboundRoot() {

        String baseURL = "http://" + ipAddress + ":" + port;

        // Create a router restlet.
        Router router = new Router(getContext());

        // Attach the resources to the router.
        router.attach(baseURL + "/files", DepResource.class);   

        // Return the root router
        return router;
    }   
}

错误:

Jul 20, 2019 2:00:03 PM org.restlet.engine.http.connector.HttpClientHelper start 
INFO: Starting the default HTTP client 
Exception in thread "main" Internal Server Error (500) - Internal Server Error 
at org.restlet.resource.ClientResource.handle(ClientResource.java:870) 
at org.restlet.resource.ClientResource.post(ClientResource.java:1209) 
at Client.main(Client.java:44)

标签: restlet

解决方案


这个问题很不清楚,但如果你问如何返回 JsonRepresentation ,那么你不能返回 avoid而是JsonRepresentation看到:

public class DepResource extends ServerResource {
    String jsonString="{}";
    @Post
    public JsonRepresentation acceptJsonRepresentation(JsonRepresentation entity) {
        // do stuff and return a JsonRepresentation object
        Representation representation = new JsonRepresentation(jsonString);
        return representation;
    }
}

推荐阅读