<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Maven Plugin &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/maven-plugin/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 10 Feb 2024 15:41:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to make multipart requests with files through autogenerated code defined in OpenAPI</title>
		<link>https://codepills.com/how-to-make-multipart-requests-with-files-through-autogenerated-code-defined-in-openapi/</link>
					<comments>https://codepills.com/how-to-make-multipart-requests-with-files-through-autogenerated-code-defined-in-openapi/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Thu, 01 Feb 2024 15:55:58 +0000</pubDate>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[Auto-generated Code]]></category>
		<category><![CDATA[Maven Plugin]]></category>
		<category><![CDATA[Multipart File Uploads]]></category>
		<category><![CDATA[OpenAPI Schema]]></category>
		<category><![CDATA[Postman Testing]]></category>
		<category><![CDATA[REST Endpoint]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1360</guid>

					<description><![CDATA[This article provides a clear tutorial on generating infrastructure code defined in OpenAPI schema for your custom Spring Boot application and handling multipart file uploads through an autogenerated REST endpoint. <a href="https://codepills.com/how-to-make-multipart-requests-with-files-through-autogenerated-code-defined-in-openapi/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>This article provides a clear tutorial on generating code infrastructure defined OpenAPI schema for your custom Spring Boot application and handling multipart file uploads through an autogenerated REST endpoint.</p>
<p><span id="more-1360"></span></p>
<h2>Introduction</h2>
<p>Let&#8217;s imagine we want to build some simple user management app separated frontend with which we will communicate through REST.</p>
<p>Also, let&#8217;s add some limitations and requirements for our user management project. We will need to use the OpenAPI schema to generate the code infrastructure, as it can help us speed up interface development between the frontend and backend. We will focus on the implementation of code infrastructure on the backend side.</p>
<p>We generally want to receive and process multiple user files through multipart form data in Spring Boot.</p>
<h2>Maven OpenAPI plugin</h2>
<p>First, we must define a plugin in the pom.xml file to generate the code infrastructure from the OpenAPI schema.</p>
<p>Here is an example of the <em>pom.xml</em> file with the plugin definition:</p>
<pre><code class="language-xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;parent&gt;
    &lt;groupId&gt;com.codepills.openapi&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-parent&lt;/artifactId&gt;
    &lt;version&gt;1.1.0&lt;/version&gt;
  &lt;/parent&gt;

  &lt;groupId&gt;com.codepills.openapi&lt;/groupId&gt;
  &lt;artifactId&gt;user-management-module&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;user-management-module&lt;/name&gt;
  &lt;description&gt;User management module&lt;/description&gt;

  &lt;properties&gt;
    &lt;java.version&gt;21&lt;/java.version&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
    &lt;version.swagger.codegen&gt;3.0.50&lt;/version.swagger.codegen&gt;
    &lt;open.api.file&gt;${project.basedir}/src/main/api-specification/OpenApi-UserManagement.yaml&lt;/open.api.file&gt;
    &lt;open.api.base&gt;com.codepills.openapi.usermanagementmodule&lt;/open.api.base&gt;
    &lt;generated-sources-path&gt;${project.build.directory}/generated-sources&lt;/generated-sources-path&gt;
    &lt;generated-sources-java-path&gt;main/java&lt;/generated-sources-java-path&gt;
  &lt;/properties&gt;

  &lt;dependencies&gt;...&lt;/dependencies&gt;

  &lt;build&gt;
    &lt;finalName&gt;${project.name}&lt;/finalName&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;io.swagger.codegen.v3&lt;/groupId&gt;
        &lt;artifactId&gt;swagger-codegen-maven-plugin&lt;/artifactId&gt;
        &lt;version&gt;${version.swagger.codegen}&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;generate-the-stuff&lt;/id&gt;
            &lt;phase&gt;generate-sources&lt;/phase&gt;
            &lt;goals&gt;
              &lt;goal&gt;generate&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
          &lt;inputSpec&gt;${open.api.file}&lt;/inputSpec&gt;
          &lt;language&gt;spring&lt;/language&gt;
          &lt;modelNameSuffix&gt;Dto&lt;/modelNameSuffix&gt;
          &lt;configOptions&gt;
            &lt;sourceFolder&gt;${generated-sources-java-path}&lt;/sourceFolder&gt;
            &lt;basePackage&gt;${open.api.base}&lt;/basePackage&gt;
            &lt;modelPackage&gt;${open.api.base}.api.model&lt;/modelPackage&gt;
            &lt;apiPackage&gt;${open.api.base}.api&lt;/apiPackage&gt;
            &lt;interfaceOnly&gt;true&lt;/interfaceOnly&gt;
            &lt;useBeanValidation&gt;true&lt;/useBeanValidation&gt;
            &lt;library&gt;spring-boot3&lt;/library&gt;
            &lt;dateLibrary&gt;custom&lt;/dateLibrary&gt;
            &lt;jakarta&gt;true&lt;/jakarta&gt;
          &lt;/configOptions&gt;
          &lt;typeMappings&gt;
            &lt;typeMapping&gt;DateTime=Instant&lt;/typeMapping&gt;
            &lt;typeMapping&gt;multipartFile=org.springframework.web.multipart.MultipartFile&lt;/typeMapping&gt;
          &lt;/typeMappings&gt;
          &lt;importMappings&gt;
            &lt;importMapping&gt;Instant=java.time.Instant&lt;/importMapping&gt;
          &lt;/importMappings&gt;
          &lt;output&gt;${generated-sources-path}&lt;/output&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;

  &lt;repositories&gt;...&lt;/repositories&gt;

&lt;/project&gt;</code></pre>
<p>As you can see, we have defined the SpringBoot module, and in it, the OpenAPI file is located at <i>/src/main/API-specification/OpenApi-UserManagement.yaml</i>. The <em>OpenApi-UserManagement.yaml</em> file will also be responsible for defining generated code infrastructure.</p>
<h2>OpenAPI User management definition YAML</h2>
<p>Let&#8217;s create the <em>OpenApi-UserManagement.yaml</em> file at <i>/src/main/api-specification/</i> relative to the location of <i>pom.xml</i></p>
<pre><code class="language-yaml">openapi: 3.0.2
info:
  title: User management module
  description: User management
  version: '0.1'
  contact:
  email: optional@email.com
servers:
  - url: https://localhost:8080/api/user-management

paths:
  /users/import:
    post:
      summary: Import User files
      tags:
        - UserManagement
      operationId: importFiles
      requestBody:
        content:
          multipart/form-data:
            schema:
              properties:
                userFiles:
                  type: array
                  items:
                    type: multipartFile
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: object
                additionalProperties:
                  type: array
                  items:
                    type: string
        '400':
          description: Bad Request</code></pre>
<p>As you can see, we have defined a simple endpoint for importing user files. The endpoint is defined as a <code>POST</code> request with a summary, tags, operationId, requestBody and responses.</p>
<p>It is essential to define the keywords for user files well. I picked up the <i>userFiles</i>, but you can use whatever you like. For example &#8211; simply <i>files</i>. The keyword will be used for the protocol to tie the files during transmission.</p>
<h2>User Management Controller</h2>
<p>Now, all we need to do is create a controller to implement the OpenAPI schema. User management schema name will be generated based on the tags in the OpenAPI schema.</p>
<p>Here is an example of the controller:</p>
<pre><code class="language-java">@RestController
@RequiredArgsConstructor
public class UserManagementController implements UserManagementApi {

  @Override
  public ResponseEntity&lt;Map&lt;&lt;tring, List<String&gt;&gt;&gt; importFiles(@RequestPart("userFiles") List&lt;MultipartFile> userFiles) {
    // ... your logic here ...
  }

}</code></pre>
<p>I will leave the implementation up to you as it is not the main focus of this article.</p>
<p>Again, notice the <i>userFiles</i> keyword in the method argument. It is the same as the keyword as defined in the OpenAPI schema.</p>
<h2>Testing with Postman</h2>
<p>Let&#8217;s run the app; the last thing will be testing the endpoint with Postman.</p>
<p>As it might not be stressed enough so far, the most important thing is to use the same keyword as defined in the OpenAPI schema. In our case, it is <i>userFiles</i>.</p>
<p><img fetchpriority="high" decoding="async" src="https://codepills.com/wp-content/uploads/2024/02/postman_multipart_request_with_multiple_files.png" alt="Postman - Multipart request with multiple files" width="665" height="319" class="size-full wp-image-1361" srcset="https://codepills.com/wp-content/uploads/2024/02/postman_multipart_request_with_multiple_files.png 665w, https://codepills.com/wp-content/uploads/2024/02/postman_multipart_request_with_multiple_files-300x144.png 300w" sizes="(max-width: 665px) 100vw, 665px" /></p>
<p>And now, if the backend is running at localhost, and we added the necessary logic for file processing into the controller, we should be able to upload multiple files through the endpoint.</p>
<p>When using Postman, you also need to define the type of multipart key. It is either the <em>String</em> or <em>File</em>. For files, use <em>File</em>, and for the object, use <em>String</em>. The following article will discuss the <a href="https://codepills.com/how-to-make-multipart-requests-with-file-and-object-through-autogenerated-code-defined-in-openapi/" title="How to make multipart requests with file and object through autogenerated code defined in OpenAPI" target="_blank" rel="nofollow noopener">implementation and use of mixed input</a>.</p>
<p>Otherwise, all you need to do is to define your endpoint <code>POST</code> request as everything else. Select <code>POST</code> request, add the URL, and in the body tab, select form-data and add the keyword and the file.</p>
<p>Add multiple files with the same key &#8220;userFiles&#8221; and send the request.</p>
<h2>Conclusion</h2>
<p>This article has shown how to implement autogenerate of code from OpenAPI schema, how to define the schema for multiple file uploads and how to implement the endpoint at the Spring Boot backend. All you need to do is run your application and use the same keyword defined in the OpenAPI schema for the file upload.</p>
<p>Did you find autogenerate OpenAPI implementation and sending files through multipart requests easy? Do you have your trick or know another way <u> to send effectively multipart requests with files</u>? Let us know in the comments below the article. We would like to hear your ideas and stories.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-make-multipart-requests-with-files-through-autogenerated-code-defined-in-openapi/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
