このコンテンツは選択した言語では利用できません。

Chapter 20. Multipart Providers


RESTEasy has rich support for the multipart/* and multipart/form-data MIME (Multipurpose Internet Mail Extension) types. The multipart MIME format passes lists of content bodies. Multiple content bodies are embedded in the one message. multipart/form-data is often found in web application HTML Form documents, and is generally used to upload files. The form-data format works like other multipart formats, except that each inlined piece of content has a name associated with it.
RESTEasy provides a custom API for reading and writing multipart types, as well as marshalling arbitrary List (for any multipart type) and Map (multipart/form-data only) objects.

20.1. Input with multipart/mixed

When you write a JAX-RS service, RESTEasy provides an interface to let you read any multipart MIME type: org.jboss.resteasy.plugins.providers.multipart.MultipartInput.
package org.jboss.resteasy.plugins.providers.multipart;

public interface MultipartInput
{
   List<InputPart> getParts();

   String getPreamble();
}

public interface InputPart
{
   MultivaluedMap<String, String> getHeaders();

   String getBodyAsString();

   <T> T getBody(Class<T> type, Type genericType) throws IOException;

   <T> T getBody(org.jboss.resteasy.util.GenericType<T> type) throws IOException;

   MediaType getMediaType();
}
Copy to Clipboard Toggle word wrap
MultipartInput is a simple interface that lets you access each part of the multipart message. Each part is represented by an InputPart interface, and is associated with a set of headers. You can unmarshal a part by calling one of the getBody() methods. The Type genericType parameter can be null, but the Class type parameter must be set. RESTEasy locates a MessageBodyReader based on the media type of the part, and the type information you pass in. The following piece of code unmarshalls XML parts into a JAXB annotated class called Customer.
   @Path("/multipart")
   public class MyService
   {
      @PUT
      @Consumes("multipart/mixed")
      public void put(MultipartInput input)
      {
         List<Customer> customers = new ArrayList...;
         for (InputPart part : input.getParts())
         {
            Customer cust = part.getBody(Customer.class, null);
            customers.add(cust);
         }
      }
   }
Copy to Clipboard Toggle word wrap
If you want to unmarshall a body part that is sensitive to generic type metadata, you can use the org.jboss.resteasy.util.GenericType class, like so:
   @Path("/multipart")
   public class MyService
   {
      @PUT
      @Consumes("multipart/mixed")
      public void put(MultipartInput input)
      {
         for (InputPart part : input.getParts())
         {
            List<Customer> cust = part.getBody(new GenericType>List>Customer<<() {});
         }
      }
   }

Copy to Clipboard Toggle word wrap
GenericType is required here because it is the only way to obtain generic type information at runtime.
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat