2016-04-04 68 views
0

從REST google api我發送郵件Content-Type: application/x-www-form-urlencodedJava,JBOSS,接受MULTIPART_FORM_DATA和JSON,西班牙語重音

------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-- 

在我的方法我消耗:

@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; 
    } 

所以對於Caño接收到的符號是Caýýo。我嘗試了很多選項,包括所有的編碼類型,但沒有成功。有人可以幫助我,或者給我一些建議,關於如何用適當的符號以一種方法接受文件和json。

+0

檢查[此](http://stackoverflow.com/questions/10226018/jboss-encoding-utf-8)。自從我使用JBOSS以來已經有一段時間了,但我記得不得不在配置文件中爲葡萄牙語字符配置一些東西。 – dambros

+0

我昨天試過了,但它仍然不起作用。 –

+0

請嘗試以下命令:@Consumes(MediaType.MULTIPART_FORM_DATA +「; charset = utf-8」) – dambros

回答

0

2天后我解決了這個問題。

在我的POM我更新了依賴於:

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

然後,我創建的類。

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; 
    } 
} 

並且所述方法:

 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")); 
     }