'How to send jsonfile in api Request

I am facing an issue while sending the json file in request for RestController, Please check the details below

I have a json file , let say test.json

{
    "PolicyNumber": "123",
    "Type": "Test",
    "Tenture": "10",
    "SDate": "10-July-2016",
    "HName": "Test User",
    "Age": "10"
}

I want to send the test.json file postman enter image description here

TestController

@RestController
public class TestController {
    
    @PostMapping(value="/uploadJsonFile", consumes= {MediaType.MULTIPART_FORM_DATA_VALUE},
             produces = {MediaType.APPLICATION_JSON_VALUE} )
    
    public ResponseEntity<String> uplaodFile(@RequestPart MultipartFile file){
        
        System.out.println("Original File Name :- "+file.getOriginalFilename());
        System.out.println("File type :- "+file.getName());
        System.out.println("File Size :- "+file.getSize()); 
        System.out.println("Content Type :- "+file.getContentType());
        //Validation file is empty
        if(file.isEmpty()) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("File is Empty Insert Data"); 
        }
        
        if(!file.getContentType().equals("application/json")) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("only json file content"); 
        }
        return ResponseEntity.ok("Upload File Sucessfully"); 
    }
}

I have tried couple of ways like below In method, if I used @RequestBody ,file is comes as null and when I used @ResponseBody or @RequestPart postman giving 400 bad request error message

I also tried to used @Controller and @RequestMapping(value="/uploadJsonFile", method= RequestMethod.POST but no luck

Pom.xml file

<properties>
        <java-version>1.8</java-version>
        <spring.version>4.3.26.RELEASE</spring.version>
        <hibernate.version>5.1.0.Final</hibernate.version>
        <springsecurity.version>4.2.15.RELEASE</springsecurity.version>
        <logback.version>1.2.3</logback.version>
        <jcl.slf4j.version>1.7.30</jcl.slf4j.version>
    </properties>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
    <display-name>TestWebApp</display-name>
    <welcome-file-list>
        <welcome-file>/WEB-INF/index.jsp</welcome-file>
    </welcome-file-list>
    

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>  
</web-app>

Spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan
        base-package="com.test.api" />
    <context:annotation-config />
 
    <context:property-placeholder
        location="classpath:application.properties" />

    <mvc:resources mapping="/resources/**"
        location="/resources/" />
        
 <mvc:annotation-driven validator="validator"/> 
 
        <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:message" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    
     
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
    </bean>
    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
        <aop:aspectj-autoproxy/>
</beans>

Note:- I have a very big json file, so can not send it as json data, need to send the json file only

Really Appriciate any help here



Solution 1:[1]

Would like to add my answer in case anyone who also faces a similar issue can use it. TestController

@RestController
public class TestController {
    
    @PostMapping("/uploadJsonFile")    
    public ResponseEntity<String> uplaodFile(@RequestParam("file") final MultipartFile file){
        
        System.out.println("Original File Name :- "+file.getOriginalFilename());
        System.out.println("File type :- "+file.getName());
        System.out.println("File Size :- "+file.getSize()); 
        System.out.println("Content Type :- "+file.getContentType());
        //Validation file is empty
        if(file.isEmpty()) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("File is Empty Insert Data"); 
        }
        
        if(!file.getContentType().equals("application/json")) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("only json file content"); 
        }
        return ResponseEntity.ok("Upload File Sucessfully"); 
    }
}

Postman request

enter image description here

Solution 2:[2]

Have you tried to modify the key name on your request? In your code, you named 'file' to your MultipartFile, but in postman you named 'type' for your key text. Try to name 'file' instead of 'type'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vicky9988
Solution 2 Adolin K.