在liferay7中开发不依赖Service Builder的对外服务类,非常简洁,只需要2点注解:
- 在类的前部定义:
@ApplicationPath("/PathXXX")
- 方法前定义:
@GET
@Path("/ActionXXX")
@Produces("text/plain")
例子:得到注册用户
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.UserLocalService;
import java.util.Collections;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; @ApplicationPath("/allUsers")
@Component(
immediate = true, property = {"jaxrs.application=true"},
service = Application.class
)
public class UsersRestService extends Application { @Override
public Set<Object> getSingletons() {
return Collections.singleton((Object)this);
} @GET
@Path("/list")
@Produces("text/plain")
public String getAllUsers() {
StringBuilder result = new StringBuilder(); for (User user : _userLocalService.getUsers(-, -)) {
result.append(user.getFullName()).append(",\n");
}
return result.toString();
} @Reference
public void setUserLocalService(UserLocalService userLocalService) {
_userLocalService = userLocalService;
} private UserLocalService _userLocalService; }
osgi语法真是简洁。