如何从邮件重定向休息服务到获取

时间:2017-05-22 04:49:34

标签: java rest redirect

我已编写代码将一个休息服务重定向到另一个休息服务。在SubmitId()方法中,我重定向另一个休息服务(getLicense()和getError()),我无法重定向它。请帮助我out ..(我正在使用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();
}

}

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

您不需要提供完整路径/DAMLicenseTool/damlicense/resource/error只需从Controller类Path开始提供URI

URI uri = new URI("/resource/error");
return Response.temporaryRedirect(uri).build();

答案 2 :(得分:0)

return“redirect:/ secondUrl”; 或者使用Httpclient来调用第二个URL