【问题标题】:Java, JBOSS, Accept MULTIPART_FORM_DATA and JSON with spanish accentJava、JBOSS、接受 MULTIPART_FORM_DATA 和带有西班牙口音的 JSON
【发布时间】:2016-07-24 05:00:51
【问题描述】:

我通过 REST google api 使用 Content-Type: application/x-www-form-urlencoded 发送 POST。

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

【问题讨论】:

  • 检查this。自从我使用 JBOSS 已经有一段时间了,但我记得必须在配置文件中为葡萄牙语字符配置一些东西。
  • 我昨天试过了,还是不行。
  • 试试下面的@Consumes(MediaType.MULTIPART_FORM_DATA + ";charset=utf-8")
  • 没有我的朋友:415:不支持的媒体类型。
  • 我不知道 RestEasy,所以我会让其他有经验的人为你解答。如果是春天,我可以帮助你。

标签: java json rest jboss resteasy


【解决方案1】:

两天后,我解决了这个问题。

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

【讨论】:

    猜你喜欢
    • 2015-01-16
    • 2013-01-31
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多