restful--002--使用jax-rs作为angular英雄服务端

源码
https://pan.baidu.com/s/1E_QQNBRWc41VUn1T6IVRlQ
3htz

tomcat文件
https://pan.baidu.com/s/1mOzBwxQkLE0-gK8jnRMJ7Q
zk4d

1、英雄资源
package com.restful.resources;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.restful.bean.Hero;
import com.restful.service.HeroService;
import com.restful.service.impl.HeroServiceImpl;

@Path("")
@Produces(MediaType.APPLICATION_JSON)
public class HeroResource {

	private HeroService heroService = new HeroServiceImpl();

	@GET
	@Path("/hero")
	public List list() {
		return heroService.list();
	}

	@GET
	@Path("/hero/{id}")
	public Hero get(@PathParam("id") String id) {
		return heroService.get(id);
	}

	@PUT
	@Path("/hero")
	public Hero add(@QueryParam("name") String name) {
		return heroService.add(name);
	}

	@POST
	@Path("/hero")
	@Consumes(MediaType.APPLICATION_JSON)
	public Hero update(Hero hero) {
		return heroService.update(hero);
	}

	@DELETE
	@Path("/hero/{id}")
	public String delete(@PathParam("id") String id) {
		return heroService.delete(id);
	}

}

2、英雄服务

接口

package com.restful.service;

import java.util.List;

import com.restful.bean.Hero;

public interface HeroService {
	
	List list();
	
	Hero get(String id);
	
	Hero add(String name);
	
	Hero update(Hero hero);
	
	String delete(String id);

}

实现

package com.restful.service.impl;

import java.util.List;

import com.restful.bean.Hero;
import com.restful.dao.HeroDao;
import com.restful.dao.impl.HeroDaoImpl;
import com.restful.service.HeroService;

public class HeroServiceImpl implements HeroService {
	
	private HeroDao heroDao = HeroDaoImpl.getInstance();

	@Override
	public List list() {
		return heroDao.list();
	}

	@Override
	public Hero get(String id) {
		return heroDao.get(id);
	}

	@Override
	public Hero add(String name) {
		return heroDao.add(name);
	}

	@Override
	public Hero update(Hero hero) {
		return heroDao.update(hero);
	}

	@Override
	public String delete(String id) {
		return heroDao.delete(id);
	}

}


3、英雄数据

package com.restful.dao;

import java.util.List;

import com.restful.bean.Hero;

public interface HeroDao {

	List list();

	Hero get(String id);

	Hero add(String name);

	Hero update(Hero hero);

	String delete(String id);
}

实现

package com.restful.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.restful.bean.Hero;
import com.restful.dao.HeroDao;

public class HeroDaoImpl implements HeroDao {

	private HeroDaoImpl() {
	}

	private static HeroDaoImpl impl = null;

	private static List heroList = new ArrayList<>();

	public static HeroDaoImpl getInstance() {
		if (impl == null) {
			impl = new HeroDaoImpl();
			initHeroList();
		}
		return impl;
	}

	@Override
	public List list() {
		return heroList;
	}

	@Override
	public Hero get(String id) {
		Hero hero = new Hero();
		for (Hero h : heroList) {
			if (h.getId().equalsIgnoreCase(id)) {
				hero = h;
				break;
			}
		}
		return hero;
	}

	@Override
	public Hero add(String name) {
		Hero hero = new Hero();
		hero.setId(getUUID());
		hero.setName(name);
		heroList.add(hero);
		return hero;
	}

	@Override
	public Hero update(Hero hero) {
		for (Hero h : heroList) {
			if (h.getId().equalsIgnoreCase(hero.getId())) {
				h.setName(hero.getName());
				break;
			}
		}
		return hero;
	}

	@Override
	public String delete(String id) {
		for (Hero h : heroList) {
			if (h.getId().equalsIgnoreCase(id)) {
				heroList.remove(h);
				break;
			}
		}
		return "success";
	}

	private static void initHeroList() {
		Hero hero = new Hero();
		hero.setId(getUUID());
		hero.setName("angular");
		heroList.add(hero);
	}

	private static String getUUID() {
		return UUID.randomUUID().toString().toLowerCase();
	}
}

4、英雄bean

package com.restful.bean;

public class Hero {
	
	private String id;
	
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

你可能感兴趣的:(restful,ja)