gpt4 book ai didi

java - org.springframework.web.bind.MissingServletRequestParameterException : Required String parameter 'text' is not present

转载 作者:行者123 更新时间:2023-12-02 01:16:08 25 4
gpt4 key购买 nike

我试图将字符串和图像从 View 发送到 Controller ,但我不断收到此错误:

String parameter'text' is not present

我尝试了一些在网上找到的解决方案,但都没有解决我的问题。

 @RequestMapping(value = "/post-create", method = RequestMethod.POST)
@ResponseBody
public String[][] createPost(@RequestParam(value="text" ) String text,
@RequestParam(value = "photo") MultipartFile body) throws IOException {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = user.getUsername();
com.project.agro.entities.User user1 = userService.getUserByUsername(username);

Image image = new Image(body.getBytes());
imageService.addImage(image);
Post post = new Post();
post.setAuthor(user1);
post.setText(text);
post.setImage(image);
postService.addPost(post);

post = postService.addPost(post);
List<Post> posts = new ArrayList<>();
posts.add(post);

return utilsService.arrayFill(posts, user1);
}

JQuery

 $("#create-post-button").click(function (e) {
e.preventDefault();
var form = $('form')[0];
var formData = new FormData(form);
$.ajax({
url: '/post-create',
data: formData,
type: 'POST',
contentType: false,
processData: false,
success: function (data) {
$("#posts-container").prepend(concatPost(data[0]));
$("#special-alert").remove();
$("#posts-container").append('' + special_alert_no_more_posts);
document.getElementById('create-post-form').reset();
$("#characters-number").text(5000);
from += 1;
$("#create-post-button").prop("disabled", true);
}
})

});

HTML

<div id="create-post-box" class="row">
<hr class=\ "middle\">
<a id="create-post-box-href"><span id="arrow-down"
class="fa fa-angle-down"></span><span id="arrow-up"
class="fa fa-angle-up hidden"></span></a>
<div id="create-post-form-div" class="hidden">
<form enctype="multipart/form-data" id="create-post-form" method="POST">
<div class="form-group">
<div class="col-sm-12">
<textarea class="form-control" maxlength="5000" rows="5" id="post-text" name="text"></textarea>
<div id="counter">
<p id="counter-line">
<span id="characters-number">5000</span> symbols left.
</p>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<label class="input-group-btn"> <span class="btn btn-primary"><span class="glyphicon glyphicon-picture"></span><input id="file" type="file" style="display: none;" name="photo" accept="image/x-png,image/gif,image/jpeg" multiple>
</span>
</label> <input type="text" class="form-control" readonly>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="button" id="create-post-button" class="btn btn-primary btn-md btn-block">Post it</button>
</div>
</div>
</form>
</div>

我试过了

@RequestParam(value="text" ,required=false)

但随后它显示:

String parameter'photo' is not present with Multipart missingservletexception

最佳答案

尝试使用@RequestBody MultipartFile body而不是@RequestParam(value = "photo") MultipartFile body

关于java - org.springframework.web.bind.MissingServletRequestParameterException : Required String parameter 'text' is not present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57658934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com