REST rest与webservicee与普通的servlet有什么不同

spring+resteasy开发webservice服务 - 推酷
spring+resteasy开发webservice服务
有一段时间没有更新博客,主要是最近一段时间自己比较迷茫,一直在思考自己以后的路该怎么走。希望大家也可以给我一些建议,谢谢!好了,回归正题,今天给大家带来的是spring+resteay开发webservice服务,不知道大家是否在这之前接触过webservice,我之前所了解的webservice是使用cxf还有axis2开发的,但是我觉得实现起来比较麻烦,而且不灵活,今天给大家介绍一种比较灵活的提供webservice服务的技术:resteasy。下面我重点讲解的resteasy常用的一些知识。我依然是结合例子给大家讲解。
package com.jrj.
import javax.ws.rs.CookieP
import javax.ws.rs.DefaultV
import javax.ws.rs.FormP
import javax.ws.rs.GET;
import javax.ws.rs.HeaderP
import javax.ws.rs.MatrixP
import javax.ws.rs.POST;
import javax.ws.rs.P
import javax.ws.rs.PathP
import javax.ws.rs.QueryP
import javax.ws.rs.core.C
import javax.ws.rs.core.HttpH
import javax.ws.rs.core.MultivaluedM
import javax.ws.rs.core.PathS
import org.jboss.resteasy.annotations.F
import org.springframework.stereotype.C
import com.alibaba.fastjson.JSONO
import com.jrj.entity.FormE
@Controller
@Path(&/content&)
public class ContentService {
* path通配符
@Path(&/pathRegular/{msg}&)
public String pathRegular(@PathParam(&msg&) String m){
* {var:.*}代表可以是任意多个路径名,如:a/b/c
* @param m
@Path(&/pathRegular1/{var:.*}/{msg}&)
public String pathRegular1(@PathParam(&msg&) String m){
* {var}代表只能有一个路径名,如:a
* @param m
@Path(&/pathRegular1/{var}/{msg}&)
public String pathRegular2(@PathParam(&msg&) String m){
* @PathParam的用法
* @param param
@Path(&/pathParam/{param}&)
public String pathParam(@PathParam(&param&) String param){
return &hello&+
* @QueryParam的用法,如:GET /queryParam/2?name=wenqiang&age=23
* @param id
* @param name
* @param age
@Path(&/queryParam/{id}&)
public String queryParam(@PathParam(&id&) int id,
@QueryParam(&name&) String name,
@QueryParam(&age&) int age){
if(id==1){
return &this is no people&;
return &name is &+name+&age is& +
* @HeadParam的用法,其中HeaderParam可以获取httpheader中的一些信息
* @param head
* @param myhead
@Path(&/headParam/{head}&)
public String headParam(@PathParam(&head&) String head,
@HeaderParam(&myhead&) String myhead){
return head + & is & +
* @MatixParam的用法(矩阵参数),如:GET:/matixParam/sex=age=23
* @param name
@Path(&/matixParam/{name}&)
public String matixParam(@PathParam(&name&) String name,
@MatrixParam(&sex&) String sex,
@MatrixParam(&age&) int age){
return &name is & + name +& age is & + age +& sex is &+
* 使用@PathParam和PathSegment来实现MatixParam
* 当矩阵参数中存在着相同的参数使用@MatixParam来获取参数会出现问题,此时应该使用@PathParam和PathSegment实现
* GET:/matixParam/sex:age:23
@Path(&/pathSegment/{info}&)
public String pathSegment(@PathParam(&info&) PathSegment info){
StringBuffer sb = new StringBuffer(&name is &);
MultivaluedMap&String, String& paraMap = info.getMatrixParameters();
* 也可以使用迭代器
/* for(Iterator&String& it = paraMap.keySet().iterator();it.hasNext();){
String temp = paraMap.get(it.next()).get(0);
String temp = paraMap.get(&name&).get(0);
sb.append(temp);
temp = paraMap.get(&age&).get(0);
sb.append(& age is & + temp);
temp = paraMap.get(&sex&).get(0);
sb.append(& sex is & + temp);
return sb.toString();
* @CookieParam的用法
* @param cookie
* @param sum
* @param sessionid
@Path(&/cookieParam/{cookieName}&)
public String cookieParam(@PathParam(&cookieName&) String cookie,
@QueryParam(&sum&) int sum,
@CookieParam(&JSESSIONID&) String sessionid){
return &cookieName is & + cookie + &sessionId is & + sessionid + & sum is & +
* @FormParam的用法,通过form表单传递值
* @param name
* @param firstname
* @param secondname
@Path(&/formParam/{name}&)
public String formParam(@PathParam(&name&) @DefaultValue(&limin&) String name,
@FormParam(&firstname&) String firstname,
@FormParam(&secondname&) String secondname){
return name+ & firstname is & + firstname + & secondname is & +
* @Form的用法,使用@Form可以把其他的@*Param的类型的参数放在同一个类中
@Path(&/form&)
public String form(@Form FormEntity entity){
return entity.toString();
* @Context的用法,@Context只允许注入以下这些类的实例:
* javax.ws.rs.core.HttpHeaders
* javax.ws.rs.core.UriInfo
* javax.ws.rs.core.Request
* javax.servlet.HttpServletRequest
* javax.servlet.HttpServletResponse
* javax.servlet.ServletConfig
* javax.servlet.ServletContext
* javax.ws.rs.core.SecurityContext
* @param headers
@Path(&/context&)
public String context(@Context HttpHeaders headers,String content){
JSONObject jsonObject = JSONObject.parseObject(content);
String name = jsonObject.getString(&name&);
int age = jsonObject.getIntValue(&age&);
return name+&,&+
大家通过看上面的例子应该发现了resteasy的使用方式和springmvc的使用方式很相似,下面我来详细讲解这些注解的意思:
1、@Path相当于springmvc中的@RequestMapping,就是标识请求路径,使用@Path来标识resteasy的请求路径,在@Path中可以含有通配符,大概就三种方式,上面代码中都有注释讲解,如果还有童鞋对于通配符不知道是什么东西,花点时间去了解一些。
2、@GET,@POST,@PUT,@DELETE这些表示请求该路径的方法,和springmvc中的RequestMethod.GET等,一般我们常用的就是@GET和@POST
3、@PathParam,这个表示的是获取路径中的参数,在路径中用{}包含
4、@QueryParam,这个表示的是路径中的参赛,用&连接
5、@HeadParam,这个表示httpheader中的数据
6、@MatixParam,这个表示的获取路径中的矩阵参数
7、@PathParam和Segment混合同样可以实现和@MatixParam一样的效果
8、@CookieParam,顾名思义获取cookie中的信息
9、@FormParam,表单传递
10、@Form,可以包含其他的@*Param
11、如果传递的是Json数据的,不需要使用注解,直接在参数列表中声明变量,然后再解析出来
12、大家可以通过使用httpclient来代码级别访问,同时为大家介绍一个chrome的httpclient插件postman
以上就是resteasy的基本开发,很简单吧,大家赶紧试试把,后续为大家带来springmvc处理请求的一些知识,敬请期待!我们下期再见!
resteasy的文档:
已发表评论数()
&&登&&&录&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
从/artifact/com.sun.jersey/jersey-bundle 下载对应版本,放到WebRoot/WEB-INF/lib下,右击,并Add to Build Path
java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
下载:asm-commons-3.3.jar& /Code/Jar/a/Downloadasmcommons33jar.htm&下载:asm-3.3.jar& /Code/Jar/a/Downloadasm33jar.htm&下载:asm-tree-3.3.jar& /Code/Jar/a/Downloadasmtree33jar.htm&
加入Build Path
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
restful项目却没有定义webservice,定义一个webservice即可。
No provider classes found.
这个是提示信息,不是错误信息。
把web.xml 里 Jersey servlet 从“com.sun.jersey.spi.container.servlet.ServletContainer” 改为 “com.sun.jersey.spi.spring.container.servlet.SpringServlet“.
另外在类上加@Provider注解,如:
import javax.ws.rs.GET;
import javax.ws.rs.P
import javax.ws.rs.P
import javax.ws.rs.QueryP
import javax.ws.rs.ext.P
@Path(&/helloWS&)
public class HelloService {
@Path(&/hello{name}&)
@Produces(&application/json&)
public String sayhello(@QueryParam(&name&) String name) {
throw new UnsupportedOperationException(&Not yet implemented.&);
java.lang.ClassNotFoundException: com.sun.jersey.spi.spring.container.servlet.SpringServlet
下载:/Code/Jar/j/Downloadjerseyspring15jar.htm
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
在&servlet& (含com.sun.jersey.spi...)里加
&init-param&
&param-name&com.sun.jersey.config.property.packages&/param-name&
&param-value&&/param-value&
&/init-param&
javax.servlet.ServletException: Error allocating a servlet instance
重启Tomcat
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
把方法上的@Produces(MediaType.TEXT_XML) &改为@Produces(&application/json&)
and MIME media type application/json was not found
下载:http://owlike.github.io/genson/&
Servlet Jersey REST Service is not available(页面提示这个错误)
Failed to classload type while reading annotation metadata. This is a non-fatal error, but certain annotation metadata may be unavailable.
java.lang.ClassNotFoundException: javax.ws.rs.Path(控制台提示这个错误)
下载:/Code/Jar/j/Downloadjsr311api111jar.htm
无法处理application/x-www-form-charset=UTF-8
jquery的ajax请求改用post,content-type设置为:application/json
Could not deserialize to type class com.sun.jersey.api.representation.Form
改Content-Type和Consumes为MediaType.APPLICATION_FORM_URLENCODE 及application/x-www-form-urlencode
The request sent by the client was syntactically incorrect (Bad Request).
这个看不出来错误,配置一下web.xml,加
&init-param&
&param-name&com.sun.jersey.config.feature.TracePerRequest&/param-name&
&param-value&true&/param-value&
&/init-param&ajax请求加:
headers: {
&X-Jersey-Trace-Accept&:true
},这时看到服务端错误再处理。
java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
发现Struts2 Core Libraries下有个antlr-2.7.2.jar
Hibernate 4.1 Core Libraries有个antlr-2.7.7.jar
删除版本低的一个。
另外,去MyEclise安装目录删除低版本的jar。
如果上述操作无效,那就去workspace里搜索antlr-2.7.2.jar并删除。
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:83555次
积分:3855
积分:3855
排名:第3973名
原创:294篇
评论:40条
(7)(19)(30)(52)(60)(21)(24)(5)(55)(9)(3)(12)(1)(5)Cant Run Simple Jersey REST Webservice Example [Solved] (Web Services forum at JavaRanch)
File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Cant Run Simple Jersey REST Webservice Example
So I am trying to run a very simple example from
of building a REST Jersey Webservice
Here is what i did
-Named the project Jersey
(Dynamic Web Project)
-Created a package "sample.hello.resources"
-Added a class called "HelloResource"
-The code of the class HelloResource is
package sample.hello.
import javax.ws.rs.GET;
import javax.ws.rs.P
import javax.ws.rs.P
import javax.ws.rs.core.MediaT
@Path("/hello")
public class HelloResource {
@Produces(MediaType.TEXT_PLAIN)
public String sayHello()
return "Hello Jersey";
-Changed the web.xml file to the following
&?xml version="1.0" encoding="UTF-8"?&
&web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&
&display-name&Jersey&/display-name&
&welcome-file-list&
&welcome-file&index.html&/welcome-file&
&/welcome-file-list&
&servlet-name&Jersey REST Service&/servlet-name&
&servlet-class&com.sun.jersey.spi.container.servlet.ServletContainer&/servlet-class&
&init-param&
&param-name&com.sun.jersey.config.property.packages&/param-name&
&param-value&sample.hello.resources&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&Jersey REST Service&/servlet-name&
&url-pattern&/rest/*&/url-pattern&
&/servlet-mapping&
&/web-app&
-I then added the project to my server ( in eclipse)
-I then typed the followoing url
And i get the following message
HTTP Status 404 - Servlet Jersey REST Service is not available
type Status report
message Servlet Jersey REST Service is not available
description The requested resource (Servlet Jersey REST Service is not available) is not available.
Any idea why i am getting "404-Service not available" in Firefox
I finally managed o solve the problem. Just needed to dump the rar libraries in the correct folder.
I have the same the problem (the only difference is that I am using sailfin instead of tomcat).
What rars did you copy and where ?
All the rar files in the lib folder of Jersey in the containers folder.
Just now ran into this problem while following the Jersey docs and finally figured out what the docs failed to mention. For those of you using
to build your web app, you need to add the following dependency:
&dependency&
&groupId&com.sun.jersey&/groupId&
&artifactId&jersey-servlet&/artifactId&
&version&1.12&/version&
&scope&runtime&/scope&
&/dependency&
Thus, for the simple hello example at , modifying it to run as a web app as mentioned at , you only need 2 dependencies:
jersey-server and jersey-servlet (this assumes that in your webapp you won't have the Main class that used grizzly to run the web service).
Your resulting WAR file should have the following JARS:
WEB-INF/lib/asm-3.1.jar
WEB-INF/lib/jersey-core-1.12.jar
WEB-INF/lib/jersey-server-1.12.jar
WEB-INF/lib/jersey-servlet-1.12.jar
Check if jersey-server library is there in the Path.
Sometimes, if your version doesn't match, then there's problem in jersey server startup.
I too faced the problem. Later with jersey-server-1.9.1.rar, the problem got solved.
I have the same problem.
"What rars did you copy and where ?" - please be more specific because I am newer (Web Services, , and Eclipse).
I put the jar libraries (not rar) in the lib folder of the "Jersy" project (see attachment).
When I type "
" URL I still got "NOT-FOUND".
what you meant when saying "All the rar files in the lib folder of Jersey in the containers folder":
- what is the container folder ?.
it in the development are (just using jar)
NOT-FOUND 404.png
I am having the same problem with another tutorial.
I am using Eclipse (STS 3.2), Tomcat 7.0.26, Jersey 1.17, JDK 1.6.
When Tomcat starts up I see the following error message:
Jul 25, :02 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /de.vogella.jersey.first threw load() exception
: com.sun.jersey.spi.container.servlet.ServletContainer
The class in question is in jersey-servlet-1.17.jar and all the jersey jars are in the
Build Path -& Libraries.
Can anyone tell me what the problem is and how to fix it?
all the jersey jars are in the Java Build Path -& Libraries.
There's your problem: build path != runtime path. The libraries need to be in the WEB-INF/lib directory of your web app.
This is a typical case where IDEs behave differently than standalone servers. I never run Tomcat in an IDE, it just complicates matters IMO.
I also copied all the jersey jars to the WEB-INF/lib folder.
I get the same error. What could be the problem?
So this is a standalone Tomcat, run independently of Eclipse, the WEB-INF/lib directory of your web app contains a jar file that has the missing class, and still you're getting that exception?
I should have given more details in the reply.
I am still running Tomcat through Eclipse IDE. I have copied the jersey jars to the WEB-INF/lib folder and to the Java Build Path -& Libraries folder of the web app.
Yet I am getting the exception.
I exported the web app as a .war file and started Tomcat independently (external to Eclipse IDE).
Now, the example works OK.
There is definitely a problem running Tomcat through Eclipse IDE.
Follow this tutorial to create a simple web service. It worked for me -
Here's the link:
subject: Cant Run Simple Jersey REST Webservice Example
Similar Threads
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winterREST webservice与普通的servlet有什么不同_百度知道
REST webservice与普通的servlet有什么不同
提问者采纳
REST webservice 指的是一种REST 服务,而普通的servlet一般是充当前端控制器来用的,不当成服务。。
其他类似问题
为您推荐:
webservice的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 web service servlet 的文章

 

随机推荐