首页 前端知识 用Jersey构建RESTful服务3--JAVA对象转成JSON输出_jersey 框架 直接接受实体类 用什么json转换器

用Jersey构建RESTful服务3--JAVA对象转成JSON输出_jersey 框架 直接接受实体类 用什么json转换器

2024-08-30 20:08:28 前端知识 前端哥 474 192 我要收藏
 user.setAge("27");  
 user.setUserId("005");  
 user.setUserName("Fmand");  
 return user;  
} 

  
 MediaType.APPLICATION\_JSON 说明输出的是JSON格式 
  


2,运行项目,浏览器输入<http://localhost:8089/RestDemo/rest/users/getUserJson>


期望获取到json的数据,此时,项目报错




org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.waylau.rest.bean.User, genericType=class com.waylau.rest.bean.User.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor T e r m i n a l W r i t e r I n t e r c e p t o r . a r o u n d W r i t e T o ( W r i t e r I n t e r c e p t o r E x e c u t o r . j a v a : 247 ) a t o r g . g l a s s f i s h . j e r s e y . m e s s a g e . i n t e r n a l . W r i t e r I n t e r c e p t o r E x e c u t o r . p r o c e e d ( W r i t e r I n t e r c e p t o r E x e c u t o r . j a v a : 162 ) a t o r g . g l a s s f i s h . j e r s e y . f i l t e r . L o g g i n g F i l t e r . a r o u n d W r i t e T o ( L o g g i n g F i l t e r . j a v a : 293 ) a t o r g . g l a s s f i s h . j e r s e y . m e s s a g e . i n t e r n a l . W r i t e r I n t e r c e p t o r E x e c u t o r . p r o c e e d ( W r i t e r I n t e r c e p t o r E x e c u t o r . j a v a : 162 ) a t o r g . g l a s s f i s h . j e r s e y . s e r v e r . i n t e r n a l . J s o n W i t h P a d d i n g I n t e r c e p t o r . a r o u n d W r i t e T o ( J s o n W i t h P a d d i n g I n t e r c e p t o r . j a v a : 103 ) a t o r g . g l a s s f i s h . j e r s e y . m e s s a g e . i n t e r n a l . W r i t e r I n t e r c e p t o r E x e c u t o r . p r o c e e d ( W r i t e r I n t e r c e p t o r E x e c u t o r . j a v a : 162 ) a t o r g . g l a s s f i s h . j e r s e y . s e r v e r . i n t e r n a l . M a p p a b l e E x c e p t i o n W r a p p e r I n t e r c e p t o r . a r o u n d W r i t e T o ( M a p p a b l e E x c e p t i o n W r a p p e r I n t e r c e p t o r . j a v a : 88 ) a t o r g . g l a s s f i s h . j e r s e y . m e s s a g e . i n t e r n a l . W r i t e r I n t e r c e p t o r E x e c u t o r . p r o c e e d ( W r i t e r I n t e r c e p t o r E x e c u t o r . j a v a : 162 ) a t o r g . g l a s s f i s h . j e r s e y . m e s s a g e . i n t e r n a l . M e s s a g e B o d y F a c t o r y . w r i t e T o ( M e s s a g e B o d y F a c t o r y . j a v a : 1154 ) a t o r g . g l a s s f i s h . j e r s e y . s e r v e r . S e r v e r R u n t i m e TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:293) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154) at org.glassfish.jersey.server.ServerRuntime TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)atorg.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)atorg.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:293)atorg.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)atorg.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)atorg.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)atorg.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)atorg.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)atorg.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)atorg.glassfish.jersey.server.ServerRuntimeResponder.writeResponse(ServerRuntime.java:571)
at org.glassfish.jersey.server.ServerRuntime R e s p o n d e r . p r o c e s s R e s p o n s e ( S e r v e r R u n t i m e . j a v a : 378 ) a t o r g . g l a s s f i s h . j e r s e y . s e r v e r . S e r v e r R u n t i m e Responder.processResponse(ServerRuntime.java:378) at org.glassfish.jersey.server.ServerRuntime Responder.processResponse(ServerRuntime.java:378)atorg.glassfish.jersey.server.ServerRuntimeResponder.process(ServerRuntime.java:368)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)


  

![](http://g.hiphotos.bdimg.com/album/s=550;q=90;c=xiangce,100,100/sign=5f3758b36509c93d03f20ef2af0689e1/7e3e6709c93d70cfade7b8b0fadcd100baa12b69.jpg?referer=70ed5c049c510fb3210e42a79527&x=.jpg)
  


此时,需要获取json转换包的支持 。


可以由多种方式实现:MOXy、JSON-P、Jackson、Jettison等,本例为Jackson。


3. jackson-all-1.9.11.jar 下载地址<http://wiki.fasterxml.com/JacksonDownload>


4. 项目中引入jackson-all-1.9.11.jar


5.在“com.waylau.rest”目录下创建RestApplication.java




package com.waylau.rest;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.filter.L

真题解析、进阶学习笔记、最新讲解视频、实战项目源码、学习路线大纲
详情关注公中号【编程进阶路】

oggingFilter;
import org.glassfish.jersey.server.ResourceConfig;

/**

  • 应用

  • @author waylau.com

  • 2014-3-18
    */
    public class RestApplication extends ResourceConfig {
    public RestApplication() {

    //服务类所在的包路径
    packages(“com.waylau.rest.resources”);
    //注册JSON转换器
    register(JacksonJsonProvider.class);

    }
    }


  
 6.修改web.xml,初始化从RestApplicaton进入应用,如下: 



### 文末

如果30岁以前,可以还不知道自己想去做什么的话,那30岁之后,真的觉得时间非常的宝贵,不能再浪费时间在一些碎片化的事情上,比如说看综艺,电视剧。一个人的黄金时间也就二,三十年,不能过得浑浑噩噩。所以花了基本上休息的时间,去不断的完善自己的知识体系,希望可以成为一个领域内的TOP。



同样是干到30岁,普通人写业务代码划水,榜样们深度学习拓宽视野晋升管理。

这也是为什么大家都说30岁是程序员的门槛,很多人迈不过去,其实各行各业都是这样都会有个坎,公司永远都缺的高级人才,只用这样才能在大风大浪过后,依然闪耀不被公司淘汰不被社会淘汰。



**269页《前端大厂面试宝典》**

包含了腾讯、字节跳动、小米、阿里、滴滴、美团、58、拼多多、360、新浪、搜狐等一线互联网公司面试被问到的题目,涵盖了初中级前端技术点。



![](https://www.qianduange.cn/upload/article/b98713ee557d94286de8afe404cb51d1.png)



**前端面试题汇总**

![](https://www.qianduange.cn/upload/article/1d691ca297c9016828aff783a701e065.png)



**JavaScript**

![](https://www.qianduange.cn/upload/article/a489904576da281d07092f043566f6dd.png)
转载请注明出处或者链接地址:https://www.qianduange.cn//article/17270.html
标签
restful
评论
发布的文章

JWT(JSON WEB TOKEN)详解

2024-09-10 23:09:36

NPM 常用命令(十二)

2024-09-10 23:09:24

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!