폼 태그의 값을 가진 json을 Spring MVC 컨트롤러에 보내고 싶다.JSON to Spring Controller
방법 제출의 형태는 내가
@RequestMapping(value="/startupload", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ReturnData startUpload(@RequestBody UploadStartVO startVO)
{...
을 다음과 같이
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
내 컨트롤러 서명이 내 스프링 구성 XML에 다음을 추가 한
var jsonStr = JSON.stringify($("#form1").serializeArray());
$.ajax({
url : "/DMS/webcontroller/startupload",
type : "POST",
// traditional : true,
contentType : "application/json; charset=utf-8",
processData:false,
//dataType : "json",
data : jsonStr,
success : function (response) {
alert('success ' + response);
},
error : function (response) {
alert('error ' + response);
},
});
다음 코드를 실행 파이어 폭스 31과 함께 바람둥이 7에서 이걸 실행할 때, 나는 415 응답을 얻고있다. 내가 확인하면 요청 도구에 Content-Type 요청이 추가되었습니다.
supplierId=1111&unregSupplier=unregSupplier&customerName=customerName&externalEntity=externalentity&application=scms&logicalGroup=demand&user=u1&password=u1&appUser=appu1&documentType=email&documentDescription=documentDescription&contactPerson=contactPerson&contactNumber=contactNumber&documentGroupName=demand&documentPurpose=documentpurpose&uploaderIP=uploaderIp&uploaderName=scmsname&uploaderCompletePath=somepath%2Fsomepath&uploadDateTime=20%2F12%2F2014
을이 마음에 들지 : 또한 요청 데이터는 다음과 같이 질의 포맷
이[{"name":"supplierId","value":"1111"},{"name":"unregSupplier","value":"unregSupplier"},{"name":"customerName","value":"customerName"},{"name":"externalEntity","value":"externalentity"},{"name":"application","value":"scms"},{"name":"logicalGroup","value":"demand"},{"name":"user","value":"u1"},{"name":"password","value":"u1"},{"name":"appUser","value":"appu1"},{"name":"documentType","value":"email"},{"name":"documentDescription","value":"documentDescription"},{"name":"contactPerson","value":"contactPerson"},{"name":"contactNumber","value":"contactNumber"},{"name":"documentGroupName","value":"demand"},{"name":"documentPurpose","value":"documentpurpose"},{"name":"uploaderIP","value":"uploaderIp"},{"name":"uploaderName","value":"scmsname"},{"name":"uploaderCompletePath","value":"somepath/somepath"},{"name":"uploadDateTime","value":"20/12/2014"}]
요청 본문을 보여줍니다
이Content-Type: application/x-www-form-urlencoded
Content-Length: 457
요청 헤더
Host: localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8082/DMS/
Cookie: JSESSIONID=C64F21EEECFC5BDF515C3DE22F51626A
Connection: keep-alive
을 보여줍니다
이 요청을 편집하고 json (디버깅 중에 jsonStr var에있는 내용)을 요청 본문에 Content-Type을 변경하고 헤더에 Content-Type을 추가하면 400 잘못된 요청이 발생합니다 !! !!
제발 도와주세요!
EDIT :: 그래서 주된 문제는 $ .ajax()가 적절한 요청을 작성하지 않는다는 것입니다. 데이터는 json이 아니며 contenttype은 application/json이 아닙니다.
편집 : 은 pom.xml 파일의 의존성을
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind-version}</version>
</dependency>
EDIT를 붙여 잊었 ::: 내가 변환 된 객체 UploadStartVO 및 ReturnData을 제거하고 String 객체로 교체하는 코드를 시도했다. 호출이 성공했지만 ajax() 메소드가 여전히 예외를 반환했지만 form 태그에 내 컨트롤러 메소드로 올바르게 전달되는 action 속성이 있습니다. 누구 아약스 전화 나던가 작동 왜 지적 할 수 있습니까 ?? Firefox 31
pls 내 대답과 함께 시도해보십시오. –
어떤 버전의 스프링을 사용하고 있습니까? –
4.0.2.RELEASE.jackson 2.3.3 – kavita