Spring MVC/简单发布请求不起作用

编程入门 行业动态 更新时间:2024-10-26 01:25:13
本文介绍了Spring MVC/简单发布请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的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/简单发布请求不起作用

本文发布于:2023-10-10 22:28:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   简单   Spring   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!