当前位置:首页 >> 网络编程

SpringMVC restful 注解之@RequestBody进行json与object转换

由于快过年的原因,项目组没有太多任务,闲来无事研究了一下spring中restful调用。发现spring竟然已经强大到如此境界,程序员已经不需要在关心在写接口的过程中数据的转换以及调用,只需要专注业务。下面我总结一下步骤及其在研究过程的遇到的问题。

步骤:

           1、git clone https://github.com/spring-guides/gs-rest-service.git 从spring官网上下载了源码

           2、进行maven编译(gradle也行)

           3、运行、访问http://localhost:8080/greeting

           4、运行结果能把对象转换为json对象返回给页面

 这时我就在思考怎样能让请求的数据自动转换为java对象呢,通过google,发现其实spring已经提供了HttpMessageConverter转换器,而且默认情况下是加载了 MappingJackson2HttpMessageConverter(json ~object转换的类)。只需要配置@RequestBody Greeting gree 即可使用。

controller层代码如下:

@RequestMapping(value = "/greeting", method = RequestMethod.POST,consumes = "application/json")
  public @ResponseBody Greeting greeting(@RequestBody Greeting gree) { 
    System.out.println(gree.getContent());
    return gree;
  }

这时候我通过谷歌的插件(postman)进行调用,死活调用不成功!

分析问题及解决问题:

这时我感觉问题的原因可能出在如下几个方面:

 1、spring默认没有加载MappingJackson2HttpMessageConverter(不知道具体加载方式)

 2、MappingJackson2HttpMessageConverter加载后不能工作(不知道不工作原因)

其实最后面导致不工作的原因是太相信spring的源码(对象没有提供set方法导致),带着这两疑问在网上海量搜索者找不到对应结果。没有办法只能从根本上找到问题原因,看spring源代码。

针对第一个问题:

    第一步:手动重写加载类型转换器

@Configuration
  @EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
  public void configureMessageConverters(List<HttpMessageConverter<"init convert is start !!!!!");
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    stringConverter.setWriteAcceptCharset(false);
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    System.out.println("init convert is stop !!!!!");
  }
}

测试发现还是不能使用,这时就更不清楚原因了。只能看默认情况下spring是怎么加载类型转换器的。结果发现在WebMvcConfigurationSupport中这个方法addDefaultHttpMessageConverters(HttpMessageConverter这个关键字反射搜索到使用地方通过判断及其跟踪找到的)中如下代码:

@SuppressWarnings("deprecation")
  protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<"htmlcode">
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage,
      MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
    MediaType contentType;
    try {
      contentType = inputMessage.getHeaders().getContentType();
    }
    catch (InvalidMediaTypeException ex) {
      throw new HttpMediaTypeNotSupportedException(ex.getMessage());
    }
    if (contentType == null) {
      contentType = MediaType.APPLICATION_OCTET_STREAM;
    }
    Class<"Reading [" + targetType + "] as \"" +
                contentType + "\" using [" + converter + "]");
          }
          return genericConverter.read(targetType, contextClass, inputMessage);
        }
      }
      if (targetClass != null) {
        if (converter.canRead(targetClass, contentType)) {
          if (logger.isDebugEnabled()) {
            logger.debug("Reading [" + targetClass.getName() + "] as \"" +
                contentType + "\" using [" + converter + "]");
          }
          return ((HttpMessageConverter<T>) converter).read(targetClass, inputMessage);
        }
      }
    }
    throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
  }

这时候发现其实已经根据HttpMessageConverter的canRead方法已经找到了对应的类型消息转换器MappingJackson2HttpMessageConverter,而且已经开始进行转换了,只是抛出了运行时异常。因为异常没有在控制台输出。我通过断点调试发现MappingJackson2HttpMessageConverter的readJavaType方法抛出运行时异常,通过源代码发现底层是用的jackson的objectMapper进行操作的,代码如下:

try {
      return this.objectMapper.readValue(inputMessage.getBody(), javaType);
    }
    catch (IOException ex) {
      throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }

如是我就把代码单独拿出来在main方法里面运行,还是不行,这时我就好定位问题了。要不是类型错误,要不是输入数据错误。仔细检查发现json数据没有问题,用jsonobject也能进行转换。这时只能判断是传入的javaType有问题导致的。如是我打开发现对象(Greeting)没有set方法,我想是不是因为此jakson没法工作呢(原理不清楚)。如是乎我给此对象提供了set方法,再运行可以了。绕了一圈终于把问题解决了,但是通过这个问题让我更加清楚了spring的restful的工作机制。