RST API

Jersey

RESTful Web Services in Java

 

In my situation, Our team want to get chinese text tokenized result by JCSEG Tokenizer. After some thoughts, I dediced to provide them a restful api to meet them demands. The jersey framwork is the first

option to my situation.

Steps

1. In Eclipse, create a dynamic web project.

2. download jersey-archive-1.18.zip  and unzip it

3. import all jars in jersey-archive-1.18/lib to the lib dir of above created web project

4. Create three class

package com.inoknok.tokenizer.rest;

import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.lionsoul.jcseg.core.JcsegException;

@Path("/token")
public class TokenizerService {

	@GET
	@Path("/{name}")
	public Response getMessage(@PathParam("name") String name)
			throws JcsegException, IOException {

		// JcsegTest demo = new JcsegTest();
		// demo.segment(name);
		String outMsg;
		outMsg = Tool.demo.segment(name);

		return Response.status(200).entity(outMsg).build();
	}
}

 

package com.inoknok.tokenizer.rest;

import java.io.IOException;
import java.io.StringReader;

import org.lionsoul.jcseg.core.ADictionary;
import org.lionsoul.jcseg.core.DictionaryFactory;
import org.lionsoul.jcseg.core.ISegment;
import org.lionsoul.jcseg.core.IWord;
import org.lionsoul.jcseg.core.JcsegException;
import org.lionsoul.jcseg.core.JcsegTaskConfig;
import org.lionsoul.jcseg.core.SegmentFactory;

public class JcsegTest {

	ISegment seg = null;

	public JcsegTest() {
		// JcsegTaskConfig config = new JcsegTaskConfig();
		String propertyFilePath = "/path/to/jcseg.properties";
		JcsegTaskConfig config = new JcsegTaskConfig(propertyFilePath);

		ADictionary dic = DictionaryFactory.createDefaultDictionary(config);

		try {
			seg = SegmentFactory.createJcseg(JcsegTaskConfig.COMPLEX_MODE,
					new Object[] { config, dic });
		} catch (JcsegException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String segment(String str) throws IOException {

		StringBuffer sb = new StringBuffer();
		sb.append("{Result:[");
		// seg.setLastRule(null);
		IWord word = null;

		boolean isFirst = true;
		seg.reset(new StringReader(str));
		while ((word = seg.next()) != null) {
			if (isFirst) {
				sb.append(word.getValue());
				isFirst = false;
			} else {
				sb.append(",");
				sb.append(word.getValue());
			}

			word = null;
		}
		sb.append("]}");

		return sb.toString();
	}

}

 

package com.inoknok.tokenizer.rest;

public class Tool {
	public static JcsegTest demo = new JcsegTest();

}

 

The web.xml content is

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>InokTokenizer</display-name>
  <display-name>REST Web Application Demo</display-name>
  <servlet>
    <servlet-name>jersey-serlvet</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>com.inoknok.tokenizer.rest</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

 

5. deploy the project to tomcat6 or tomcat7

 

6. you can access the source by curl

curl http://localhost:8080/InokTokenizer/token/world-bes,hello,google,baidu

 

 

 

http://www.tuicool.com/articles/2Y3iue

http://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/

http://blog.csdn.net/zztfj/article/details/7608991

http://www.ibm.com/developerworks/cn/xml/x-agaviREST/

 

jersey bulde jar download url : http://www.java2s.com/Code/Jar/j/jersey-bundle.htm

 

https://jersey.java.net/documentation/latest/user-guide.html

 

Java servlet

http://tutorials.jenkov.com/java-servlets/web-xml.html

 

curl

http://www.cnblogs.com/-clq/archive/2012/01/29/2330827.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值