익진
48
2019-06-13 01:15:48
3
123

ajax로 form데이터를 받으려는데


아래와같은 오류가 뜨는데 이유가 뭘까요

ajax에서 form전달하고

컨트롤러에서 @RequestBody VoTest voTest로 받으면 name값이랑 일치하는 부분들을 받을 수 있는게 아닌가요??? 

 Cannot construct instance of `com.spring.study.board.vo.VoTest 이 뜨는 이유가 무엇인가요

====================================================================

오류

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.spring.study.board.vo.VoTest` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('title=sdagsdgdsa&content=gsadgsdg'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.spring.study.board.vo.VoTest` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('title=sdagsdgdsa&content=gsadgsdg')

 at [Source: (PushbackInputStream); line: 1, column: 1]

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:240) ~[spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:225) ~[spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:201) ~[spring-webmvc-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:150) ~[spring-webmvc-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128) ~[spring-webmvc-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) [spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) [spring-web-4.3.21.RELEASE.jar:4.3.21.RELEASE]

==================================================

controller

@RequestMapping(value="/board/addNewArticle",method=RequestMethod.POST)

public String writeArticle(@RequestBody VoTest articleVo) {

logger.info("============= wrtiteArticle ===============");

return "/board/listArticle";

}

===================================================================

jsp

function sendArticle(){

var data = $('#articleForm').serialize()

$.ajax({

url:  '${contextPath}/board/addNewArticle',

type: 'POST',

data:  JSON.stringify(data),

contentType: 'application/json',

            success : function(data){

            alert(data);

            },

error : function(xhr, status) {

                alert(xhr + " : " + status);

            }

});

}

   <tr> <td align="right">글제목: </td>

<td colspan="2"><input type="text" size="67"  maxlength="500" id="title" name="title" /></td>

 </tr>

<tr><td align="right" valign="top"><br>글내용: </td>

<td colspan=2><textarea id="content" name="content" rows="10" cols="65" maxlength="4000"></textarea> </td>

  </tr>

======================================================================

VoTest

private int articleNo;

private String title;

private String content;

private String writeMemberId;

getter/setter

=======================================================================

pom

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.9.5</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-core</artifactId>

<version>2.9.5</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.module</groupId>

<artifactId>jackson-module-parameter-names</artifactId>

<version>2.9.5</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.datatype</groupId>

<artifactId>jackson-datatype-jsr310</artifactId>

<version>2.9.5</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.datatype</groupId>

<artifactId>jackson-datatype-jdk8</artifactId>

<version>2.9.5</version>

</dependency>

<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

<version>2.9.5</version>

</dependency>

============================================

0
0
  • 답변 3

  • 사는게힘듭니다
    1k
    2019-06-13 07:35:41

    폼태그의 속성을 id=로하면 시리얼라이즈로 데이터전송이 안됩니다 name속성으로바꾸시면되요

    0
  • 사는게힘듭니다
    1k
    2019-06-13 07:37:30

    혹은 컨트롤러 메소드 매개변수로

    produces=application/jsom;charset=utf-8 

    0
  • 익진
    48
    2019-06-13 11:28:21

     

    메소드 매개변수에 consumes과 produces를 넣어두면 결국

    Content type 'application/json' not supported 이 뜹니다

    라이브러리 버전 다 맞추고 context설정부분에 컨버터를 설정해도 뜨는군요....

    그리고

    아작스에

    var data = $('#articleForm').serialize()

    data:  JSON.stringify(data),

    contentType: 'application/json',

    이렇게 해두면 consumes과 produces를 안넣어도 컨버터가 변환해주는것이 아닌가요??

    어디서부터 잘못알고있는지 몰라서 너무나 힘들군요 ㅠㅠㅠㅠㅠㅠㅠ

    무튼 답변 감사합니당



    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.