org.springframework.http.converter.HttpMessageNotReadableException调用rest post call

时间:2017-08-08 13:30:07

标签: java json spring rest

这是我的客户端代码

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity responseEntity = restTemplate.postForObject(appConfig.getRestUrl(),processFiles, ResponseEntity.class);

这是我的Rest控制器代码:

@RequestMapping(value = "/v1/processfiles/", method = RequestMethod.POST)
public ResponseEntity addProcessFiles(@RequestBody List<ProcessFiles> processFiles) {
    try {
        processFilesDao.save(processFiles);
        return ResponseEntity.status(HttpStatus.OK).body("Success");
    } catch (Exception e) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failure occured at server side");
    }

如果我在post call中使用String.class作为返回类型,则相同的代码可以工作。但不能使用Response实体类。如果我还需要客户端同样的响应实体

,我做错了什么
  

org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:无法识别的令牌&#39;成功&#39;:期待(&#39; true&#39;,&#39; false&#39;或& #39;空&#39);嵌套异常是com.fasterxml.jackson.core.JsonParseException:无法识别的令牌&#39;成功&#39;:期待(&#39; true&#39;,&#39; false&#39;或&#39; null& #39)    在[来源:java.io.PushbackInputStream@15778da9; line:1,专栏:15]       在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:244)       在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:229)       在org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:96)       在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)       在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)       在org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)       在com.mmx.ta2s3.serviceImpl.Ta2S3ServiceImpl.callRestService(Ta2S3ServiceImpl.java:170)       在com.mmx.ta2s3.Ta2S3Application.task(Ta2S3Application.java:48)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor $ LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)       at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor $ LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)       at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)       在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)       在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)       在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:303)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)       在com.mmx.ta2s3.Ta2S3Application.main(Ta2S3Application.java:38)   引起:com.fasterxml.jackson.core.JsonParseException:无法识别的令牌&#39;成功&#39;:期待(&#39; true&#39;,&#39; false&#39;或&#39; null& #39)    在[来源:java.io.PushbackInputStream@15778da9; line:1,专栏:15]       at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702)       at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558)       at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3528)       在com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2686)       at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:878)       在com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:772)       at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3834)       at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3783)       at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2922)       在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241)       ......还有31个

2 个答案:

答案 0 :(得分:0)

试试这个:

@RequestMapping(value = "/v1/processfiles/", method = RequestMethod.POST)
public ResponseEntity addProcessFiles(@RequestBody List<ProcessFiles> processFiles) {
try {
    processFilesDao.save(processFiles);
    return new ResponseEntity<>("Success", HttpStatus.OK);
} catch (Exception e) {
    return new ResponseEntity<>("Failure occured at server side", HttpStatus.INTERNAL_SERVER_ERROR);
}

答案 1 :(得分:0)

我不确定,但我认为它无法将“Success”字符串转换为ResponseEntity,而是使用字符串泛型类型:

@RequestMapping(value = "/v1/processfiles/", method = RequestMethod.POST)
public ResponseEntity<String> addProcessFiles(@RequestBody List<ProcessFiles> processFiles) {