how to make spring mvc convert the json array to arraylist(form submit with upload file)?
I was using below code to submit a form with fileupload to the backend spring mvc controller.
function submit() {
var formData = new FormData();
var fileField = document.querySelector("input[type='file']");
formData.append('mail', document.getElementsByName('mail')[0].value);
formData.append('applicationName', 'myapp');
// please remove the attachment if no file attached
formData.append('attachment', fileField.files[0]);
const releaseNotes = [{
type: "java",
overview: "good"
},
{
type: "java2",
overview: "good2"
}]
formData.append('releaseNotes', releaseNotes)
fetch('springapi/addform', {
method: 'POST',
body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', JSON.stringify(response)));
}
The controller looks like below.
@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody UserInfo save( UserInfo userInfo, HttpServletRequest request) throws RestException {
myService.save(userInfo);
return userInfo;
}
The object looks like below.
public class UserInfo {
private Long ID;
private String mail;
private String applicationName;
private List<ReleaseNote> releaseNotes;
}
When I ran this demo, it will report below exception.
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'releaseNotes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.xxx.rest.jpa.entity.ReleaseNote] for property 'releaseNotes[0]': no matching editors or conversion strategy found] (.java:24) in http-bio-8080-exec-1
ps: If there is no need to use fileupload, I can solve this issue by using RestController and post the json to backend by below.
fetch(url,
{method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: json })
But since there is fileupload, so I have to use FormData instead of passing json to backend. How to solve this issue? Thanks.
spring spring-mvc
add a comment |
I was using below code to submit a form with fileupload to the backend spring mvc controller.
function submit() {
var formData = new FormData();
var fileField = document.querySelector("input[type='file']");
formData.append('mail', document.getElementsByName('mail')[0].value);
formData.append('applicationName', 'myapp');
// please remove the attachment if no file attached
formData.append('attachment', fileField.files[0]);
const releaseNotes = [{
type: "java",
overview: "good"
},
{
type: "java2",
overview: "good2"
}]
formData.append('releaseNotes', releaseNotes)
fetch('springapi/addform', {
method: 'POST',
body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', JSON.stringify(response)));
}
The controller looks like below.
@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody UserInfo save( UserInfo userInfo, HttpServletRequest request) throws RestException {
myService.save(userInfo);
return userInfo;
}
The object looks like below.
public class UserInfo {
private Long ID;
private String mail;
private String applicationName;
private List<ReleaseNote> releaseNotes;
}
When I ran this demo, it will report below exception.
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'releaseNotes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.xxx.rest.jpa.entity.ReleaseNote] for property 'releaseNotes[0]': no matching editors or conversion strategy found] (.java:24) in http-bio-8080-exec-1
ps: If there is no need to use fileupload, I can solve this issue by using RestController and post the json to backend by below.
fetch(url,
{method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: json })
But since there is fileupload, so I have to use FormData instead of passing json to backend. How to solve this issue? Thanks.
spring spring-mvc
Could you also post yourReleaseNote
class?
– Sven Hakvoort
Nov 22 '18 at 9:27
add a comment |
I was using below code to submit a form with fileupload to the backend spring mvc controller.
function submit() {
var formData = new FormData();
var fileField = document.querySelector("input[type='file']");
formData.append('mail', document.getElementsByName('mail')[0].value);
formData.append('applicationName', 'myapp');
// please remove the attachment if no file attached
formData.append('attachment', fileField.files[0]);
const releaseNotes = [{
type: "java",
overview: "good"
},
{
type: "java2",
overview: "good2"
}]
formData.append('releaseNotes', releaseNotes)
fetch('springapi/addform', {
method: 'POST',
body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', JSON.stringify(response)));
}
The controller looks like below.
@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody UserInfo save( UserInfo userInfo, HttpServletRequest request) throws RestException {
myService.save(userInfo);
return userInfo;
}
The object looks like below.
public class UserInfo {
private Long ID;
private String mail;
private String applicationName;
private List<ReleaseNote> releaseNotes;
}
When I ran this demo, it will report below exception.
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'releaseNotes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.xxx.rest.jpa.entity.ReleaseNote] for property 'releaseNotes[0]': no matching editors or conversion strategy found] (.java:24) in http-bio-8080-exec-1
ps: If there is no need to use fileupload, I can solve this issue by using RestController and post the json to backend by below.
fetch(url,
{method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: json })
But since there is fileupload, so I have to use FormData instead of passing json to backend. How to solve this issue? Thanks.
spring spring-mvc
I was using below code to submit a form with fileupload to the backend spring mvc controller.
function submit() {
var formData = new FormData();
var fileField = document.querySelector("input[type='file']");
formData.append('mail', document.getElementsByName('mail')[0].value);
formData.append('applicationName', 'myapp');
// please remove the attachment if no file attached
formData.append('attachment', fileField.files[0]);
const releaseNotes = [{
type: "java",
overview: "good"
},
{
type: "java2",
overview: "good2"
}]
formData.append('releaseNotes', releaseNotes)
fetch('springapi/addform', {
method: 'POST',
body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', JSON.stringify(response)));
}
The controller looks like below.
@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody UserInfo save( UserInfo userInfo, HttpServletRequest request) throws RestException {
myService.save(userInfo);
return userInfo;
}
The object looks like below.
public class UserInfo {
private Long ID;
private String mail;
private String applicationName;
private List<ReleaseNote> releaseNotes;
}
When I ran this demo, it will report below exception.
default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'releaseNotes'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.xxx.rest.jpa.entity.ReleaseNote] for property 'releaseNotes[0]': no matching editors or conversion strategy found] (.java:24) in http-bio-8080-exec-1
ps: If there is no need to use fileupload, I can solve this issue by using RestController and post the json to backend by below.
fetch(url,
{method: 'post',
headers: {
'Accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: json })
But since there is fileupload, so I have to use FormData instead of passing json to backend. How to solve this issue? Thanks.
spring spring-mvc
spring spring-mvc
asked Nov 22 '18 at 7:59
liam xuliam xu
1,09031935
1,09031935
Could you also post yourReleaseNote
class?
– Sven Hakvoort
Nov 22 '18 at 9:27
add a comment |
Could you also post yourReleaseNote
class?
– Sven Hakvoort
Nov 22 '18 at 9:27
Could you also post your
ReleaseNote
class?– Sven Hakvoort
Nov 22 '18 at 9:27
Could you also post your
ReleaseNote
class?– Sven Hakvoort
Nov 22 '18 at 9:27
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426264%2fhow-to-make-spring-mvc-convert-the-json-array-to-arraylistform-submit-with-uplo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426264%2fhow-to-make-spring-mvc-convert-the-json-array-to-arraylistform-submit-with-uplo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you also post your
ReleaseNote
class?– Sven Hakvoort
Nov 22 '18 at 9:27