如何在 api 请求中发送 json 文件
How to send jsonfile in api Request
我在发送请求 RestController 的 json 文件时遇到问题,请查看下面的详细信息
我有一个 json 文件,假设 test.json
{
"PolicyNumber": "123",
"Type": "Test",
"Tenture": "10",
"SDate": "10-July-2016",
"HName": "Test User",
"Age": "10"
}
我要发送test.json文件邮递员
测试控制器
@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");
}
}
我试过如下几种方法
在方法中,如果我使用 @RequestBody
,文件为空,当我使用 @ResponseBody
或 @RequestPart
邮递员给出 400 bad request
错误消息
我也尝试使用 @Controller
和 @RequestMapping(value="/uploadJsonFile", method= RequestMethod.POST
但没有成功
Pom.xml 文件
<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>
注意:- 我有一个非常大的 json 文件,所以不能将其作为 json 数据发送,只需要发送 json 文件
非常感谢这里的任何帮助
您是否尝试过根据您的要求修改密钥名称?
在您的代码中,您将 'file' 命名为您的 MultipartFile,但在邮递员中,您将 'type' 命名为您的关键文本。
尝试命名 'file' 而不是 'type'
想添加我的答案,以防同样面临类似问题的人可以使用它。
测试控制器
@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");
}
}
邮递员请求
我在发送请求 RestController 的 json 文件时遇到问题,请查看下面的详细信息
我有一个 json 文件,假设 test.json
{
"PolicyNumber": "123",
"Type": "Test",
"Tenture": "10",
"SDate": "10-July-2016",
"HName": "Test User",
"Age": "10"
}
我要发送test.json文件邮递员
测试控制器
@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");
}
}
我试过如下几种方法
在方法中,如果我使用 @RequestBody
,文件为空,当我使用 @ResponseBody
或 @RequestPart
邮递员给出 400 bad request
错误消息
我也尝试使用 @Controller
和 @RequestMapping(value="/uploadJsonFile", method= RequestMethod.POST
但没有成功
Pom.xml 文件
<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>
注意:- 我有一个非常大的 json 文件,所以不能将其作为 json 数据发送,只需要发送 json 文件
非常感谢这里的任何帮助
您是否尝试过根据您的要求修改密钥名称? 在您的代码中,您将 'file' 命名为您的 MultipartFile,但在邮递员中,您将 'type' 命名为您的关键文本。 尝试命名 'file' 而不是 'type'
想添加我的答案,以防同样面临类似问题的人可以使用它。 测试控制器
@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");
}
}
邮递员请求