Spring MVC / Simple Post request does not work
In my spring MVC app, I'm trying to make a simple Post request but it doesn't work because of the body of my request. I got nested objects in my params and Spring throws this exception :
org.springframework.beans.InvalidPropertyException: Invalid property 'contrats[0][nomFichier]' of bean class [fr.mnh.signweb.dto.InitSignatureRQDTO]: Property referenced in indexed property path 'contrats[0][nomFichier]' is neither an array nor a List nor a Map; returned value was [fr.mnh.signweb.dto.ContratDTO@1c74fdf]
Here is the json sent to the request : (I didn't show the values)
And here is my object DTO :
@NoArgsConstructor
public class InitSignatureRQDTO {
@Getter
@Setter
private String referenceFournisseur;
@Getter
@Setter
private String produit;
@Getter
@Setter
private String civilite;
@Getter
@Setter
private String nom;
@Getter
@Setter
private String prenom;
@Getter
@Setter
private String email;
@Getter
@Setter
private String telephone;
@Getter
@Setter
private String rue;
@Getter
@Setter
private String complementRue;
@Getter
@Setter
private String codePostal;
@Getter
@Setter
private String ville;
@Getter
@Setter
private String pays;
@Getter
@Setter
private List<ContratDTO> contrats;
@Getter
@Setter
private String messageSms;
}
And :
@NoArgsConstructor
public class ContratDTO {
@Getter
@Setter
private String nomFichier;
/*@Getter
@Setter
private byte[] fichier;*/
}
Here is my controller :
@RequestMapping(value = "/initSign", method = RequestMethod.POST)
public ResponseEntity launchSign(InitSignatureRQDTO initSignatureRQDTO) {
System.out.println(initSignatureRQDTO);
}
I tried to use @RequestBody like :
public ResponseEntity launchSign(@RequestBody InitSignatureRQDTO initSignatureRQDTO)
But it doesn't work. I have the x-www-form-urlencoded;charset=UTF-8 not supported exception.
EDIT : When using :
@RequestMapping(value = "/initSign", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity launchSign(InitSignatureRQDTO initSignatureRQDTO) {
I got these logs :
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing POST request for [/initSign]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /initSign
DEBUG: org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
DEBUG: org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'appServlet': assuming HandlerAdapter completed request handling
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
You are sending JSON content in POST request body, so you need to use content type as application/json
in your request.
x-www-form-urlencoded;charset=UTF-8
is to be used when you are submitting a form.
"I have the x-www-form-urlencoded;charset=UTF-8 not supported exception." - I think here's the clue.
When you want to send the data such as your InitSignatureRQDTO
, you would want to use application/json
. This tells the server that you are posting JSON data as in:
{ Name : 'myname', email: 'myemail@email'}
Whereas www-form-urlencoded
is used mostly when sending a small number of params. It will notify the server that you will be encoding the parameters in the URL.
More details here.
However, if you still want to use www-form-urlencoded
, you might want to notify your control to accept that by something like:
@RequestMapping(value = "/initSign", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
链接地址: http://www.djcxy.com/p/48468.html