java - 如何将休息服务从 post 重定向到 get

标签 java rest redirect

我已经编写了将一个休息服务重定向到另一个休息服务的代码。在 SubmitId() 方法中,我正在重定向另一个休息服务( getLicense() 和 getError()),但我无法重定向它。请帮助我输出..(我正在使用 JAXRS)

 @Path("/resource")
 public class DAMLicenseResource {
 @POST
    @Path("/submittree")
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
    public Response SubmitId(ArrayList<String> selectedIds) throws JAXBException, IOException, URISyntaxException{  
    DAMLicenseService damLicenseService = new DAMLicenseService();
    DAMLicenseOutput damLicenseOutput = damLicenseService.dupSubmittedId(selectedIds);


    if(damLicenseOutput.isError()){
     java.net.URI location1 = new java.net.URI("/DAMLicenseTool/damlicense/resource/error");
        return Response.temporaryRedirect(location1).build();
    }
    else{
        java.net.URI location1 = new java.net.URI("/DAMLicenseTool/damlicense/resource/download");
        return Response.temporaryRedirect(location1).build();   

    }
 }



@POST
@Path("/download")
@Produces("text/plain")
public Response getLicense(){
    System.out.println("response api");
    File file = new File("license.txt");
    //write to this file
    try {
        System.out.println("response api");
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("license");
        fileWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ResponseBuilder response = Response.ok((Object) file);
    //System.out.println("response api");
    response.header("Content-Disposition",
        "attachment; filename=\"license.txt\"");
    return response.build();
}
@POST
@Path("/error")
@Produces("text/plain")
public Response getErrorMsg(){
    System.out.println("error");
    return Response.status(200).entity("failed").build();
}

}

最佳答案

首先,重定向在 REST 中并不正确,REST 不应该决定流程,请尝试其他方式从 POST 调用 GET。

java.net.URI location = new java.net.URI("../download"); 

暂时尝试一下,看看是否有帮助。

关于java - 如何将休息服务从 post 重定向到 get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44104849/

相关文章:

c# - 无法添加 mandrill-webhook

c++ - 从 CPPRest 库(即 casablanca)获得的结果中提取基本 STL 字符串?

javascript - Android Webview 无法直接通过 getElementById().click() 重定向到网站

Java ws Jboss 7 SOAP 头

java - 优雅地关闭 spring 批处理并在失败的情况下恢复批处理

java - Eloquent 地打印 "time between"两次打印

java - 您如何访问传递给 CompletableFuture allOf 的已完成 future ?

php - YII 在 REST GET 请求中获取请求体

ruby-on-rails-3 - Rails 重定向问题 - Hartl 的 Rails 教程 10.2.3

php - 重定向到资源路由时路由不存在 [Laravel]