2015-05-12 28 views
0

相同的資源POST方法我想不是在相同的資源類Restlet框架的一個POST方法越多,如下圖所示:電話比在Restlet框架

public class Myclass extends ServerResource { 

    private Logger log = Logger.getLogger(LoginResource.class.getName()); 

    @Post 
    public Representation createUser(final Representation representation) throws IOException { 
     .......................... 
     ................................ 
    } 

    /** 
     * This class will help in creating a person. 
     * @return representation Representation 
     * @param representation Representation 
     * @throws IOException IOException 
     */ 
    @Post 
    public Representation createAllUser(final Representation representation) throws IOException { 
     ............... 
     ............................... 
     return new JacksonRepresentation<>("Success"); 
    } 
} 

怎麼做呢?請根據我的問題提供一些解決方案。請幫幫我。

回答

1

我不知道你到底想做什麼。從您的問題中不太清楚的是選擇用於處理請求的方法(調用一個或另一個帶註釋的方法)。你想在有效負載中使用查詢參數,頭文件或其他東西嗎?

對於查詢參數,你可以在註釋級別使用的東西,如下所述:

@Post("?myparam=something") 
public Representation createUser(final Representation representation) throws IOException { 

對於其他情況下,我認爲你應該使用處理路由到一個單一註解的方法正確的處理方法。類似的東西,如果你想使用自定義標題x-action

@Post 
public Representation handleAction(Representation representation) 
            throws IOException { 
    Series<Header> headers = (Series<Header>) 
     getRequestAttributes().get("org.restlet.http.headers"); 

    String actionHeader = headers.getFirstValue("x-action", "single"); 
    if ("single".equals(actionHeader)) { 
     return handleAction1(representation); 
    } else { 
     return handleAction2(representation); 
    } 
} 

此外,你可以看看關於這個方式鏈接來實現與方法POST休息和多的Restlet行動:https://templth.wordpress.com/2015/03/20/handling-multiple-actions-for-a-post-method/

希望它可以幫助你, 蒂埃裏