2016-04-04 2 views
0

Von einem REST Google API bin ich sendind POST mit Content-Type: application/x-www-form-urlencoded.Java, JBOSS, Akzeptieren MULTIPART_FORM_DATA und JSON mit spanischem Akzent

------WebKitFormBoundary 
Content-Disposition: form-data; name="model" 
Content-type: application/json 

{ 
    "placeId":2, 
    "reportDate":"2016-03-10T05:00:00.000Z", 
    "form":{ 
    "apply" :"NO", 
"microbasin": { 
    "id": 1, 
    "name": "Caño Rubiales" 
    } 
    } 
} 
------WebKitFormBoundary-- 

In meiner Methode, die ich verbrauchen:

@POST 
    @Consumes(MediaType.MULTIPART_FORM_DATA) 
    public Response create (@Context UriInfo uriInfo, 
          @Context HttpServletRequest req, 
          MultipartFormDataInput input) throws IOException 
    { 


     List<InputPart> l = input.getFormDataMap().get("model"); 

     String str = new String (l.get(0).getBodyAsString().getBytes("iso-8859-1"), "UTF-8"); 

     System.out.println(str); 

     InputStream file = input.getFormDataPart("file", new GenericType<InputStream>() {}); 

     return null; 
    } 

So das empfangene Symbol für CañoCaýýo ist. Ich habe viele Optionen mit allen Kodierungstypen ausprobiert, aber ohne Erfolg. Kann mir jemand bitte helfen oder mir einen Rat geben, wie man Datei und JSON in nur einer Methode mit den richtigen Symbolen akzeptieren kann.

+0

Check [diese] (http://stackoverflow.com/questions/10226018/jboss-encoding-utf-8). Es ist eine Weile her, seit ich JBOSS benutzt habe, aber ich erinnere mich daran, etwas in den Konfigurationsdateien für portugiesische Zeichen konfigurieren zu müssen. – dambros

+0

Ich habe gestern versucht, aber es funktioniert immer noch nicht. –

+0

Probieren Sie die folgenden '@Consumes (MediaType.MULTIPART_FORM_DATA +"; Zeichensatz = UTF-8 ")' – dambros

Antwort

0

Nach 2 Tagen habe ich das Problem gelöst.

In meinem pom ich die Abhängigkeit aktualisiert:

<dependency> 
     <groupId>org.jboss.resteasy</groupId> 
     <artifactId>resteasy-multipart-provider</artifactId> 
     <version>2.3.5.Final</version> 
     <scope>provided</scope> 
    </dependency> 

Dann habe ich die Klasse.

import javax.ws.rs.WebApplicationException; 
import javax.ws.rs.ext.Provider; 

import org.jboss.resteasy.annotations.interception.ServerInterceptor; 
import org.jboss.resteasy.core.ResourceMethod; 
import org.jboss.resteasy.core.ServerResponse; 
import org.jboss.resteasy.plugins.providers.multipart.InputPart; 
import org.jboss.resteasy.spi.Failure; 
import org.jboss.resteasy.spi.HttpRequest; 
import org.jboss.resteasy.spi.interception.PreProcessInterceptor; 

@Provider 
@ServerInterceptor 
public class ChilaPreProcessInterceptor implements PreProcessInterceptor 
{ 
    @Override 
    public ServerResponse preProcess (HttpRequest request, 
             ResourceMethod resourceMethod) 
      throws Failure, WebApplicationException 
    { 
     request.setAttribute(InputPart.DEFAULT_CONTENT_TYPE_PROPERTY, "*/*; charset=UTF-8"); 
     return null; 
    } 
} 

Und die Methode:

 public String getBodyPartAsString (List<InputPart> parts) throws IOException 
    { 
       InputPart part = parts.get(0); 
       String value = part.getBody(String.class, null); 

     return value; 
    } 

     @POST 
     @Consumes(MediaType.MULTIPART_FORM_DATA) 
     public Response create (@Context UriInfo uriInfo, 
           @Context HttpServletRequest req, 
           MultipartFormDataInput input) throws IOException, ParseException 
     { 

       Map<String, List<InputPart>> formParts = input.getFormDataMap(); 

       if (!formParts.containsKey("model")) 
        { 
         throw new IllegalArgumentException("Cannot create document due to param missing (model)"); 
        } 

       //Parsea los datos y los pone en el DTO 
       String str = getBodyPartAsString(formParts.get("model")); 
     }