Java 绿色标记选项(内容类型) 现在更改黄色标记区域中的搜索内容类型
就我而言:Java 绿色标记选项(内容类型) 现在更改黄色标记区域中的搜索内容类型,java,json,spring-mvc,postman,Java,Json,Spring Mvc,Postman,就我而言: invoice\u id\u ls(键)包含json数据 文档包含文件数据 placed\u金额包含普通文本字符串 正文>二进制文件>选择文件 事实上,我需要传递一些值,并在邮递员处备案一项服务,这可能是负责人吗?设置什么标题?这个问题还要求上传json数据,这在本回答中没有涉及。正确的方法是:文件+(json)?你得到的是500。你能分享更多细节吗?你在那里设置了哪些6个标题?这个解决方案适合我。没有通过任何标题,工作正常。谢谢传递JSON对我来说是个棘手的部分。:)哈,聪明-改变
invoice\u id\u ls
(键)包含json
数据文档
包含文件
数据placed\u金额
包含普通文本
字符串正文>二进制文件>选择文件
事实上,我需要传递一些值,并在邮递员处备案一项服务,这可能是负责人吗?设置什么标题?这个问题还要求上传json数据,这在本回答中没有涉及。正确的方法是:文件+(json)?你得到的是500。你能分享更多细节吗?你在那里设置了哪些6个标题?这个解决方案适合我。没有通过任何标题,工作正常。谢谢传递JSON对我来说是个棘手的部分。:)哈,聪明-改变了我的端点:)有点难看但超级方便你能解释一下你在说什么吗@ChrisKoston@GauravSharma您是如何使用表单数据设置JSON的。您可以分享输入请求吗?为什么
POST
?你说的“不起作用”是什么意思?你为什么不把你的代码给我们看,让我们很容易地调试这个问题呢?我得到的文本部分总是String
。为什么它不映射到我的DTO@PostMapping(value=“/byImageFile”,consumes={“multipart/form data”})公共响应量postMap(@RequestPart(“imageFile”)MultipartFile imageFile,@RequestPart(“fieldsToExtract”)RequestDto RequestDto)
我根本无法从邮递员那里发送请求。在这种情况下,我找到了解决方案。它不会自动映射到RequestDto
。我把它当作一个字符串,解析Json
,并将它显式映射到RequestDto
。@Green Checkout Kubilay关于PUT
的回答。这是一个很好的答案。。这也可以用于在多部分负载中必须上载多个文件和json负载的用例。如果您想要发送文件和json数据,这是最正确的答案。示例显示选择一个文件,然后将JSON数据作为单个JSON字符串值传递,或者将值拆分,这些示例都不起作用(可能取决于端点,但我无法验证这一点)。这里还提到了:您能告诉我使用了哪两个标题吗?内容类型:application/json Authorization:bearer(yourTokenString)是的,它有效。谢谢我使用了以下代码并进行了工作:@PostMapping(value=Constant.API_INITIAL+“/uploadFile”)public UploadFileResponse uploadFile(@RequestParam(“file”)MultipartFile file file,String jsonFileVo){FileUploadVo FileUploadVo=null;try{FileUploadVo=new ObjectMapper().readValue(jsonFileVo,FileUploadVo.class);}catch(异常e){e.printStackTrace();}您可以将contactDtoString的内容类型设置为application/json,您可以对每个键执行此操作您是正确的,因为此操作的UI对未启动的用户不可见。感谢您提供的可视化版本!如果我有一个XML正文负载和附件,该怎么办?gce的回答似乎是说您可以轻松地完成此操作—不够?我不确定。您可能有通过学习多部分mime格式,了解如何将附件和XML组合成单个文档。这并不太复杂,您可以手动创建多部分mime正文。两年后,UI没有任何改进。您能告诉我如何实现get()吗有邮递员接收文件响应吗?说了这么多,解决了我的问题,将[]
添加到文件上传参数的名称中,例如图像[]
文件[]
上传[]
这就是我要找的。我弄不明白为什么它在POST
上工作,而不是PUT.
之前。谢谢!!文件名与要包含在所附JSON中的文件有什么关系?
/**
* Upload single file using Spring Controller.
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(
@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
HttpServletRequest request,
HttpServletResponse response) {
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "tmpFiles");
if (!dir.exists()) {
dir.mkdirs();
}
// Create the file on server
File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
System.out.println("Server File Location=" + serverFile.getAbsolutePath());
return null;
} catch (Exception e) {
return null;
}
}
}
{ description: { spanish: 'hola', english: 'hello' } }
ContactDto contactDto = new ObjectMapper().readValue(yourJSONString, ContactDto.class);
id = request.args.get('id')
@PostMapping(value = Constant.API_INITIAL + "/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file,String jsonFileVo) {
FileUploadVo fileUploadVo = null;
try {
fileUploadVo = new ObjectMapper().readValue(jsonFileVo, FileUploadVo.class);
} catch (Exception e) {
e.printStackTrace();
}
@RequestMapping(value = "/executesampleservice", method = RequestMethod.POST,
consumes = {"multipart/form-data"})
@ResponseBody
public boolean yourEndpointMethod(
@RequestPart("properties") @Valid ConnectionProperties properties,
@RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
return projectService.executeSampleService(properties, file);
}
formData = new FormData();
formData.append("file", document.forms[formName].file.files[0]);
formData.append('properties', new Blob([JSON.stringify({
"name": "root",
"password": "root"
})], {
type: "application/json"
}));
public function sendImage(Request $request)
{
$image=new ImgUpload;
if($request->hasfile('image'))
{
$file=$request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$file->move('public/upload/userimg/',$filename);
$image->image=$filename;
}
else
{
return $request;
$image->image='';
}
$image->save();
return response()->json(['response'=>['code'=>'200','message'=>'image uploaded successfull']]);
}