public class ApiDao {
public String getName() {
return "mock";
}
}
ApiService
@Path("s1")
@Component
@Scope("request")
public class ApiService {
private int count;
@Autowired
private ApiDao apiDao;
@GET
@Produces(MediaType.APPLICATION_XML)
public List<City> getCity(@DefaultValue("shanghai") @QueryParam("cname") String cityName) {
count++;//这里虽然scope设置的是request,但实际上只有一个对象count会一直累加 ????
List<City> a = new ArrayList<City>();
for (int i = 0; i < 10; i++) {
City c = new City();
c.setName(cityName+ " " + i+" "+count);
c.setZone("zone " + i + apiDao.getName());
a.add(c);
}
return a;
}
City
@XmlRootElement //加上这个才能解析成json 或 xml
public class City {
private String name;
private String zone;
ApiService2
@Path("s2")
@Component
@Scope("request")
public class ApiService2 {
@Autowired
private ApiDao apiDao;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Person getCity(@DefaultValue("shanghai") @QueryParam("cname") String cityName) {
Person p = new Person();
p.setAge(22);
p.setName("lich");
City c = new City();
c.setName(cityName);
c.setZone("zone " + apiDao.getName());
p.setCity(c);
return p;
}
输出
http://localhost:8080/axisspring/api/s1