Jersey
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