0

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) enter image description here

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
  • 1
    Sorry about offtopic but looks like you use Lombok. In this case you can apply \@Getter, \@Setter annotations for a class. There is no need to apply them for all class members. – mroman Feb 26 '18 at 17:31

2 Answers2

0

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.

Kedar Joshi
  • 1,441
  • 11
  • 17
  • I've edited my question to show you what's going on when using consumes = Json – whySoSerious Feb 26 '18 at 16:03
  • @whySoSerious You need to send your `Request` with header `content-type: application/json` – Kedar Joshi Feb 26 '18 at 16:12
  • I used this header, but no mapping was done. Indeed it worked but I have null values in my java object. – whySoSerious Feb 26 '18 at 16:19
  • You are one step closer. Can you post raw JSON which is sent ? I think your JSON looks something like `{"test":{"civilite":""}}` from the console log. It should be `{"civilite":""}`. i.e. without wrapping `test` object – Kedar Joshi Feb 26 '18 at 16:25
  • I have the following message : DEBUG: org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Failed to resolve argument 0 of type 'fr.mnh.signweb.dto.InitSignatureRQDTO' org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'referenceFournisseur': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'referenceFournisseur': was expecting ('true', 'false' or 'null') – whySoSerious Feb 26 '18 at 16:31
  • The json was successfully built, there is not "test" ^^ – whySoSerious Feb 26 '18 at 16:31
  • Without actual JSON it's impossible to tell for sure. Can you include actual JSON content ? – Kedar Joshi Feb 26 '18 at 16:35
  • I found what the solution, I had to use JSON.stringify in order to make it work. Thanks man ;) – whySoSerious Feb 26 '18 at 16:36
0

"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)
tryingToLearn
  • 10,691
  • 12
  • 80
  • 114