java rest tomcat,使用Eclipse,Tomcat和Jersey的Java中的Restful WebServices

How to create simple webserver in Java using Eclipse, Tomcat and Jersey i.e steps to follow?

We are creating simple webserver using the below links:

but we got an error like this:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

解决方案

Have maven running. Then run this command(press enter if it asks sth):

mvn archetype:generate -DgroupId=com.test.rest -DartifactId=test -DarchetypeArtifactId=maven-archetype-webapp

It will create you a simple webapp. Now create the source package as src/main/java/com/test/rest, and create a simple class as following with a name "test" in it:

package com.test.rest;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.core.Response;

@Path("/test")

public class test{

@GET

@Path("/{param}")

public Response getMsg(@PathParam("param") String msg) {

String output = "Jersey say : " + msg;

return Response.status(200).entity(output).build();

}

}

At that point you should get errors, resolve them by adding this dependency to your pom:

com.sun.jersey

jersey-server

1.8

you can run a dummy "mvn clean install" so that maven will download the repository and your errors will disappear.

Now, go to webapp/WEB-INF and configure your web.xml as follows:

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Restful Web Application

jersey-serlvet

com.sun.jersey.spi.container.servlet.ServletContainer

com.sun.jersey.config.property.packages

com.test.rest

1

jersey-serlvet

/rest/*

here we said which classes to be loaded and also gave a small prefix with "/rest". so your webservice will start with this prefix.

Now you are ready, build the app, and add the jar file under tomcat/webapps folder. when you run your tomcat you can reach to your webservice via:

(url_to_tomcat_server/jar_name/prefix_at_web_xml/prefix_at_java_rest_class/dummy_text_requested_byclass)

localhost:8080/test/rest/test/blabla

Note: tested and running

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值