RESTWebService实现之一 : Jetty+Jersey(嵌入式Jetty方式)

说明:本文采用采用Jersey这个Java REST标准来实现,Jersey融入Jetty的方法,参考自Jersey的UserGuide。

UserGuide的URI为:https://jersey.github.io/documentation/latest/index.html

本文补足了从UserGuide到实现的Maven项目的差距,展示了一个完整的例子。

一、实现代码

1.主类 实现代码

package my.restful;
import java.net.URI;
import org.eclipse.jetty.server.Server;//jetty包
import javax.ws.rs.core.UriBuilder;//jersey-container-jetty-http
import org.glassfish.jersey.server.ResourceConfig;//
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;//jersey-container-jetty-http  + jersey-hk2这两个坐标缺一不可
public class Main {

    public static void main(String[] args) throws Exception {
    	
    	URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
    	ResourceConfig config = new ResourceConfig(MyResource.class);//jersey-server
    	Server server = JettyHttpContainerFactory.createServer(baseUri, config);//pom
    	System.out.println("JettyServer Running on 127.0.0.1:8080");
    	server.start();

    }
}

2.资源类实现代码

package my.restful;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

二、Maven pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>My.Demo.Restful</groupId>
  <artifactId>Jersey-JettyEmbed</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mytest</name>
  <description>JettyEmbedServer with Jersey</description>
  <dependencies>
    <dependency>
  		<groupId>org.eclipse.jetty</groupId>
  		<artifactId>jetty-servlet</artifactId>
  		<version>9.4.8.v20171121</version>
  	</dependency>
    <dependency>
    	<groupId>org.glassfish.jersey.containers</groupId>
    	<artifactId>jersey-container-jetty-http</artifactId>
    	<version>2.26</version>
    </dependency>
    <dependency>
   		<groupId>org.glassfish.jersey.inject</groupId>
    	<artifactId>jersey-hk2</artifactId>
    	<version>2.26</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.5.1</version>
			<inherited>true</inherited>
			<configuration>
				<source>1.7</source>
				<target>1.7</target>
			</configuration>
            </plugin>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>exec-maven-plugin</artifactId>
			<version>1.6.0</version>
			<executions>
				<execution>
					<goals>
						<goal>java</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<mainClass>my.restful.Main</mainClass>
			</configuration>
		</plugin>
    </plugins>
  </build>
</project>

三、相关错误以及解决方法

1. MyResource  cannot be resolved to a type

    这说明源文件的编码有问题,将源文件删除重新建立。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值