在我的春季MVC应用程序中,我正在尝试制作一个简单的Post请求,但由于我的请求正文,它不起作用。我在params中得到了嵌套对象,Spring抛出了这个异常:
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]
这是我的对象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,如:
public ResponseEntity launchSign(@RequestBody InitSignatureRQDTO initSignatureRQDTO)
但它不起作用。我有x-www-form-urlencoded; charset = UTF-8不支持异常。
编辑: 使用时:
@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
答案 0 :(得分:0)
您正在POST请求正文中发送JSON内容,因此您需要在请求中将内容类型用作application/json
。
x-www-form-urlencoded;charset=UTF-8
。
答案 1 :(得分:0)
“我有x-www-form-urlencoded; charset = UTF-8不支持异常。” - 我认为这是线索。
如果您要发送InitSignatureRQDTO
等数据,则需要使用application/json
。
这告诉服务器您正在发布JSON数据,如下所示:
{ Name : 'myname', email: 'myemail@email'}
而www-form-urlencoded
主要用于发送少量参数时。它会通知服务器您将对URL中的参数进行编码。
更多详情here。
但是,如果您仍想使用www-form-urlencoded
,则可能需要通知您的控件接受以下内容:
@RequestMapping(value = "/initSign", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)