在我的Spring MVC应用程序中,我试图发出一个简单的Post请求,但是由于请求的主体而无法正常工作.我在参数中嵌套了对象,Spring抛出了这个异常:
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]这是发送给请求的json :(我没有显示值)
Here is the json sent to the request : (I didn't show the values)
这是我的对象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; }然后:
@NoArgsConstructor public class ContratDTO { @Getter @Setter private String nomFichier; /*@Getter @Setter private byte[] fichier;*/ }这是我的控制人:
@RequestMapping(value = "/initSign", method = RequestMethod.POST) public ResponseEntity launchSign(InitSignatureRQDTO initSignatureRQDTO) { System.out.println(initSignatureRQDTO); }我试图像使用@RequestBody一样:
I tried to use @RequestBody like :
public ResponseEntity launchSign(@RequestBody InitSignatureRQDTO initSignatureRQDTO)但是它不起作用.我有x-www-form-urlencoded; charset = UTF-8不支持的异常.
But it doesn't work. I have the x-www-form-urlencoded;charset=UTF-8 not supported exception.
使用时:
@RequestMapping(value = "/initSign", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity launchSign(InitSignatureRQDTO initSignatureRQDTO) {我得到了这些日志:
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推荐答案
您正在POST请求正文中发送JSON内容,因此您需要在请求中使用内容类型作为application/json.
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用于提交表单.
更多推荐
Spring MVC/简单发布请求不起作用
发布评论