Restlet-Convert表单编码体到Pojo

时间:2017-03-14 09:59:55

标签: java restlet

我正在使用restlet2.3构建Web服务并访问用户输入(content-type = application / x-www-form-urlencoded),即key1 = value1& key2 = value2 我们在ServerResource上以form.getFirstValue("key1")...

的形式阅读此内容
public class ServerResource{
@Post()
doPost(Form form){
form.getParameter("key1")
}

}

但是我不想要这个,相反我希望通过restlet将其转换为pojo

即pojo看起来像

class InputRequest{
String key1;
String key2

getKey1(){

}
}

并在服务器资源中读取pojo

中的值
public class ServerResource{

@Post()
doPost(InputRequest request){
request.getKey1()
}

}

所以,我的问题是,restlet有内置转换器这样做,还是我们需要编写自己的转换器并注册restlet。

2 个答案:

答案 0 :(得分:0)

如果您使用json正文的帖子,我有一个答案,不确定它是否适用于Form

这是您的资源方法:

@Post
public Representation insert(GeneralTO to) {
  .
  .
  .
}

这是你的转移对象,请记住你必须有它的设置者:

public class GeneralTO {

  private int id;
  private String name;

  public int getId() {
    return brand_id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

希望有所帮助

答案 1 :(得分:0)

对我来说,只有在覆盖org.restlet.resource.Resource.toObject(Representation, Class<T>)方法后才能实现这一点。

假设这是我的POJO:

package com.blogspot.javarestlet.form2pojo.server;

public class MyPOJO implements java.io.Serializable{

    private static final long serialVersionUID = 1L;
    private String myName;
    public String getMyName() {
        return myName;
    }
    public void setMyName(String myName) {
        this.myName = myName;
    }
    public MyPOJO() {
    }
}

这是我的资源(没有覆盖org.restlet.resource.Resource.toObject(Representation, Class<T>)):

package com.blogspot.javarestlet.form2pojo.server;

import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class MyResource extends ServerResource{
    @Post("form:txt")
    public String postFormTxt(MyPOJO myPojo){
        return "Hello "+myPojo.getMyName();
    }
}

这是我的服务器应用程序(独立):

package com.blogspot.javarestlet.form2pojo.server;

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

public class ServerApp extends Application {

    /**
     * When launched as a standalone application.
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8080);
        component.getDefaultHost().attach(new ServerApp());
        component.start();
    }

    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/a", MyResource.class);
        return router;
    }

}

我得到的输出是:

Java 1.7和Restlet 2.3

C:\Users\abhishek>curl -i -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: text/plain" "http://localhost:8080/a" -d "myName=Abhishek"
HTTP/1.1 422
Content-type: text/html; charset=UTF-8
Content-length: 541
Server: Restlet-Framework/2.3.9
Accept-ranges: bytes
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Date: Thu, 16 Mar 2017 07:08:43 GMT

<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Unprocessable Entity</p>
<p>The server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the contained instructions
</p>
<p>You can get technical details <a href="http://www.webdav.org/specs/rfc2518.html#STATUS_422">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>

Java 1.6和Restlet 2.0:

C:\Users\abhishek>curl -i -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: text/plain" "http://localhost:8080/a" -d "myName=Abhishek"
HTTP/1.1 415 Unsupported Media Type
Date: Thu, 16 Mar 2017 07:23:43 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 554
Content-Type: text/html; charset=UTF-8

<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Unsupported Media Type</p>
<p>The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method</p
>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>

但是当我将资源改为此时:

Java 6,Restlet 2.0.15

package com.blogspot.javarestlet.form2pojo.server;

import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

public class MyResource extends ServerResource{
    @SuppressWarnings("unchecked")
    @Override
    protected <T> T toObject(Representation r, Class<T> c) throws ResourceException {
        if(MyPOJO.class.equals(c)){
            try {
                MyPOJO myPojo = (MyPOJO) c.newInstance();
                Form form = new Form(r.getText());
                myPojo.setMyName(form.getFirstValue("myName"));
                return (T) myPojo;
            } catch (Exception e) {
                new ResourceException(e);
            }
        }
        return super.toObject(r, c);
    }
    @Post("form:txt")
    public String postFormTxt(MyPOJO myPojo){
        return "Hello "+myPojo.getMyName();
    }
}

Java 7,Restlet 2.3

package com.blogspot.javarestlet.form2pojo.server;

import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.Post;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

public class MyResource extends ServerResource{
    @SuppressWarnings("unchecked")
    @Override
    public <T> T toObject(Representation r, Class<T> c) throws ResourceException {
        if(MyPOJO.class.equals(c)){
            try {
                MyPOJO myPojo = (MyPOJO) c.newInstance();
                Form form = new Form(r.getText());
                myPojo.setMyName(form.getFirstValue("myName"));
                return (T) myPojo;
            } catch (Exception e) {
                new ResourceException(e);
            }
        }
        return super.toObject(r, c);
    }
    @Post("form:txt")
    public String postFormTxt(MyPOJO myPojo){
        return "Hello "+myPojo.getMyName();
    }
}

我得到了所需的输出:

C:\Users\abhishek>curl -i -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: text/plain" "http://localhost:8080/a" -d "myName=Abhishek"
HTTP/1.1 200 OK
Date: Thu, 16 Mar 2017 07:29:24 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.0.15
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 14
Content-Type: text/plain; charset=UTF-8

Hello Abhishek

如果您想避免在每个资源中覆盖toObject方法,那么有两种选择:

  1. 拥有适用于所有资源的公共父级&amp;覆盖该父类中的toObject
  2. (我没有尝试过第二个)toObject(Representation, Class<T>, Resource)中有一个org.restlet.service.ConverterService方法。尝试覆盖该方法,然后通过调用org.restlet.Application.setConverterService(ConverterService)
  3. 将其添加到您的应用程序中

    注意:还有另一种toObject(Representation)方法。我没有尝试改变它。