RD063
RD063

Reputation: 43

Client for Multipart form data to Rest WS with HTML as response

I`m trying call a web service which returns an html page of that application , how to handle this in java client so that that html should be returned to my application UI. I tried with temporary redirect. below is my code :

final MultiPart multiPart = new FormDataMultiPart()
            .field("msg", espXML, MediaType.APPLICATION_XML_TYPE)
            .field("obj", "", MediaType.TEXT_PLAIN_TYPE);
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);


    return Response.temporaryRedirect(new URI("https://10.10.10.62:8080/abcde/1.2/wstest/"))
          //  .status(302)
            .entity(multiPart).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
            .header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With")
            .header("Access-Control-Allow-Credentials", "true")
            .build();

SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.sun.jersey.multipart.FormDataMultiPart, and Java type class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type application/x-www-form-urlencoded was not found

Upvotes: 1

Views: 397

Answers (1)

rieckpil
rieckpil

Reputation: 12021

You have to annotate the MediaType of your Response like:

@POST
@Produces({MediaType.TEXT_HTML})
public InputStream yourFormMethod(...) {
   // your processing with your MultiPart
   File f = getHtmlFile();
   return new FileInputStream(f);
}

or

@POST
@Produces({MediaType.TEXT_HTML})
public String yourFormMethod(...) {
   // your processing with your MultiPart
   String yourHtml = "<head>...</head>";
   return yourHtml;
}

And make sure you use the JAX-RS @Produces not the CDI one.

Upvotes: 1

Related Questions