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

这是我的客户端代码

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调用中使用String.class作为返回类型,则相同的代码可以工作 . 但不能使用Response实体类 . 如果我需要客户端同样的响应实体,我做错了什么

org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:无法识别的标记'Success':期待('true','false'或'null');嵌套异常是com.fasterxml.jackson.core.JsonParseException:无法识别的标记'Success':在[来源:java.io.PushbackInputStream@15778da9;期待('true','false'或'null');在org.springframework.http.converter.readJavaType(AbstractJackson2HttpMessageConverter.java:244)org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:229)中的行:1,列:15] org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:96)位于org.springframework.web.client.RestTemplate.execute的org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655) (RestTemplate.java:613)位于com.mmx的com.mmx.ta2s3.serviceImpl.Ta2S3ServiceImpl.callRestService(Ta2S3ServiceImpl.java:170)的org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380) . 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(D在org.springframework的org.springframework.beans.factory.factory.InitDestroyAnnotationBeanPostProcessor $ LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)的java.lang.reflect.Method.invoke(Method.java:498)中的elegatingMethodAccessorImpl.java:43)位于org.springframework.beans.factory.support的org.springframework.beans.factory.annotationDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)中的.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor $ LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) .AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(摘要AutowireCapableBeanFactory.java:555)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)at at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)位于org.springframework.beans.factory的org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) . org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:)中的org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)中的support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197): 867)在组织的org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) .springframework.boot.SpringApplication.refresh(SpringApplication.java:693)org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)at 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:无法识别的标记'Success':在[来源:java.io.PushbackInputStream@15778da9;期待('true','false'或'null'); line:1,column:15] at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702)at atcom.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558)位于com.fasterxml.jackson.core的com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3528) . json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2686)at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:878)at 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)

2 years ago

试试这个:

@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);
}

2 years ago

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

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