maven 创建 jersey 项目

eclipse + maven + com.sun.jersey 创建 restful api

如果没找到 jersey archetype, 下载 maven 的 archetype xml, 然后导入 archetypes

eclipse + maven + com.sun.jersey 创建 restful api

运行

右击 main.java -> Run As -> Java Application

eclipse + maven + com.sun.jersey 创建 restful api

如果 pom.xml 报错: Missing artifact com.sun.jersey:jersey-client:jar:${jersey-
version}, 则需要修改 jersey 版本号, 找到 pom.xml 中的

    <properties>
        <jersey-version>${jersey-version}</jersey-version>
    </properties>

改成

    <properties>
        <jersey-version>1.19.1</jersey-version>
    </properties>

运行成功,在 Console 栏位下会显示

eclipse + maven + com.sun.jersey 创建 restful api

在浏览器输入 http://localhost:9998/application.wadl, 看到可访问的 api

eclipse + maven + com.sun.jersey 创建 restful api

然后在浏览器输入 http://localhost:9998/myresource

eclipse + maven + com.sun.jersey 创建 restful api

支持返回 json 数据对象

在 pom.xml 添加

		<dependency>
			<groupId>com.owlike</groupId>
			<artifactId>genson</artifactId>
			<version>0.99</version>
		</dependency>

在 java 文件中就可直接返回对象

    @GET
    @Path("hello")
    @Produces(MediaType.APPLICATION_JSON)
    public UserInfo hello(){
    	UserInfo user =  new UserInfo();
    	user._id = "id";
    	user._name = "haha";
    	return user;
    }
    
    public class UserInfo{
    	 String _id;
    	 String _name;
    	public String getId(){
    		return this._id;
    	}
    	public void setId(String id){
    		this._id= id;
    	}
    	public String getName(){
    		return this._name;
    	}
    	public void setName(String name){
    		this._name = name;
    	}
    }

eclipse + maven + com.sun.jersey 创建 restful api

相关文章:

  • 2022-01-05
  • 2022-01-17
  • 2021-09-03
  • 2021-08-11
  • 2021-07-02
  • 2022-01-19
猜你喜欢
  • 2021-09-02
  • 2022-01-01
  • 2022-12-23
  • 2021-05-04
  • 2021-05-13
  • 2021-11-23
  • 2021-10-22
相关资源
相似解决方案