object用法 to和return to的用法?

HTML &object& 标签25640人阅读
由于快过年的原因,项目组没有太多任务,闲来无事研究了一下spring中restful调用。发现spring竟然已经强大到如此境界,程序员已经不需要在关心在写接口的过程中数据的转换以及调用,只需要专注业务。下面我总结一下步骤及其在研究过程的遇到的问题。
&&&&&&&&&& 1、git clone
从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());
这时候我通过谷歌的插件(postman)进行调用,死活调用不成功!
分析问题及解决问题:
这时我感觉问题的原因可能出在如下几个方面:
&1、spring默认没有加载MappingJackson2HttpMessageConverter(不知道具体加载方式)
&2、MappingJackson2HttpMessageConverter加载后不能工作(不知道不工作原因)
其实最后面导致不工作的原因是太相信spring的源码(对象没有提供set方法导致),带着这两疑问在网上海量搜索者找不到对应结果。没有办法只能从根本上找到问题原因,看spring源代码。
针对第一个问题:
& & 第一步:手动重写加载类型转换器
&&& @Configuration
&&& @EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
&&& public void configureMessageConverters(List&HttpMessageConverter&?&& messageConverters) {
&& &&& &System.out.println(&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&?&& messageConverters) {
&& &&& &StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
&& &&& &stringConverter.setWriteAcceptCharset(false);
&& &&& &messageConverters.add(new ByteArrayHttpMessageConverter());
&& &&& &messageConverters.add(stringConverter);
&& &&& &messageConverters.add(new ResourceHttpMessageConverter());
&& &&& &messageConverters.add(new SourceHttpMessageConverter&Source&());
&& &&& &messageConverters.add(new AllEncompassingFormHttpMessageConverter());
&& &&& &if (romePresent) {
&& &&& &&& &messageConverters.add(new AtomFeedHttpMessageConverter());
&& &&& &&& &messageConverters.add(new RssChannelHttpMessageConverter());
&& &&& &if (jaxb2Present) {
&& &&& &&& &messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
&& &&& &if (jackson2Present) {
&& &&& &&& &messageConverters.add(new MappingJackson2HttpMessageConverter());
&& &&& &else if (jacksonPresent) {
&& &&& &&& &messageConverters.add(new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter());
已经加载了相应的默认转换器。断点调试说明默认配置是没有问题的。
只能说明是第二个问题导致的,但是不知道为什么导致这个问题(json数据问题,还是其他问题),在不知道问题的情况下,只能看request请求过来,转换器是怎么工作的。因为本人对spring不是特别了解,所以不知其原理。在这种情况下还是只能根据(HttpMessageConverter)关键类找到相应使用地方。以经验进行判断和调试。发现在AbstractMessageConverterMethodArgumentResolver中的readWithMessageConverters方法是request请求过来进行类型转换的处理方法。代码如下:
protected &T& Object readWithMessageConverters(HttpInputMessage inputMessage,
&& &&& &&& &MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
&& &&& &MediaType contentT
&& &&& &try {
&& &&& &&& &contentType = inputMessage.getHeaders().getContentType();
&& &&& &catch (InvalidMediaTypeException ex) {
&& &&& &&& &throw new HttpMediaTypeNotSupportedException(ex.getMessage());
&& &&& &if (contentType == null) {
&& &&& &&& &contentType = MediaType.APPLICATION_OCTET_STREAM;
&& &&& &Class&?& contextClass = methodParam.getContainingClass();
&& &&& &Class&T& targetClass = (Class&T&) ResolvableType.forType(targetType,
&& &&& &&& &&& &ResolvableType.forMethodParameter(methodParam)).resolve();
&& &&& &for (HttpMessageConverter&?& converter : this.messageConverters) {
&& &&& &&& &if (converter instanceof GenericHttpMessageConverter) {
&& &&& &&& &&& &GenericHttpMessageConverter&?& genericConverter = (GenericHttpMessageConverter&?&)
&& &&& &&& &&& &if (genericConverter.canRead(targetType, contextClass, contentType)) {
&& &&& &&& &&& &&& &if (logger.isDebugEnabled()) {
&& &&& &&& &&& &&& &&& &logger.debug(&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进行操作的,代码如下:
&& &&& &&& &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的工作机制。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:37229次
排名:千里之外
(1)(1)(1)(5)(1)LinQ To Object 基本用法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
LinQ To Object 基本用法
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 object用法 的文章

 

随机推荐