AngularJS-22 : RestService call
Rest classes
-------------------
UserService.java
UserEntity.java
UserDao.java
UI
------------------
angular.min.js
Script.js
myHTML.html
############################################################################
angular.min.js
-------------------------------------
Script.js
------------------------------------
/// <reference path="angular.min.js" />
var myAppModule = angular.module("myAppModule",[]).
controller("myDataController",function ($scope,$http,$log) {
$http({
method: 'GET',
url: 'UserService'}) //'resources/UserService/users'
.then(function (response) {
$scope.data = response.data;
$log.info(response);
},
function (reason) {
$scope.error = reason
});
});
myHTML.html
--------------------------------------------
<!DOCTYPE html>
<html ng-app="myAppModule">
<head>
<script type="text/javascript" src="resources/js/angular.min.js"></script>
<script type="text/javascript" src="resources/js/Script.js"></script>
</head>
<body ng-controller="myDataController">
<div>
Response data : {{ data }}
</div>
</table>
</body>
</html>
UserService.java
----------------------------------
package com.siri.impl;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
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.core.Context;
import javax.ws.rs.core.MediaType;
@Path("/UserService") // a path for the web service using @Path annotation to
// the UserService.
public class UserService {
UserDao userDao = new UserDao();
private static final String SUCCESS_RESULT="<result>success</result>";
private static final String FAILURE_RESULT="<result>failure</result>";
@GET
@Path("/users") // to specify a path for the particular web service method
// using @Path annotation to method of UserService.
@Produces(MediaType.APPLICATION_JSON)
public List<UserEntity> getUsers1jahdfhad() {
return userDao.getAllUsers();
}
@GET
@Path("/users/{userid}")
@Produces(MediaType.APPLICATION_JSON)
public UserEntity getUser(@PathParam("userid") int userid) {
return userDao.getUser(userid);
}
@PUT
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String createUser(@FormParam("id") int id, @FormParam("name") String name,
@FormParam("profession") String profession, @Context HttpServletResponse servletResponse)
throws IOException {
UserEntity user = new UserEntity(id, name, profession);
int result = userDao.addUser(user);
if (result == 1) {
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
@POST
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String updateUser(@FormParam("id") int id, @FormParam("name") String name,
@FormParam("profession") String profession, @Context HttpServletResponse servletResponse)
throws IOException {
UserEntity user = new UserEntity(id, name, profession);
int result = userDao.updateUser(user);
if (result == 1) {
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
@DELETE
@Path("/users/{userid}")
@Produces(MediaType.APPLICATION_XML)
public String deleteUser(@PathParam("userid") int userid) {
int result = userDao.deleteUser(userid);
if (result == 1) {
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
@OPTIONS
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public String getSupportedOperations() {
return "<operations>GET, PUT, POST, DELETE</operations>";
}
}
UserEntity.java
----------------------------------
package com.siri.impl;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="userEntity")
public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
private int userId;
private String userName;
private String profession;
public UserEntity(){
}
public UserEntity(int userId, String userName, String profession) {
this.userId = userId;
this.userName = userName;
this.profession = profession;
}
@XmlElement(name="UserIdElement")
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@XmlElement(name="UserNameElement")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@XmlElement(name="ProfessionElement")
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserEntity other = (UserEntity) obj;
if (profession == null) {
if (other.profession != null)
return false;
} else if (!profession.equals(other.profession))
return false;
if (userId != other.userId)
return false;
if (userName == null) {
if (other.userName != null)
return false;
} else if (!userName.equals(other.userName))
return false;
return true;
}
}
UserDao.java
-----------------------------------
package com.siri.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<UserEntity> getAllUsers() {
List<UserEntity> userList = null;
try {
File file = new File("Users.dat");
if (!file.exists()) {
UserEntity user = new UserEntity(1, "Mahesh", "Teacher");
userList = new ArrayList<UserEntity>();
userList.add(user);
saveUserList(userList);
} else {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
userList = (List<UserEntity>) ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return userList;
}
public UserEntity getUser(int id) {
List<UserEntity> users = getAllUsers();
for (UserEntity user : users) {
if (user.getUserId() == id) {
return user;
}
}
return null;
}
public int addUser(UserEntity pUser) {
List<UserEntity> userList = getAllUsers();
boolean userExists = false;
for (UserEntity user : userList) {
if (user.getUserId() == pUser.getUserId()) {
userExists = true;
break;
}
}
if (!userExists) {
userList.add(pUser);
saveUserList(userList);
return 1;
}
return 0;
}
public int updateUser(UserEntity pUser) {
List<UserEntity> userList = getAllUsers();
for (UserEntity user : userList) {
if (user.getUserId() == pUser.getUserId()) {
int index = userList.indexOf(user);
userList.set(index, pUser);
saveUserList(userList);
return 1;
}
}
return 0;
}
public int deleteUser(int id) {
List<UserEntity> userList = getAllUsers();
for (UserEntity user : userList) {
if (user.getUserId() == id) {
int index = userList.indexOf(user);
userList.remove(index);
saveUserList(userList);
return 1;
}
}
return 0;
}
private void saveUserList(List<UserEntity> userList) {
try {
File file = new File("Users.dat");
FileOutputStream fos;
fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment