Commit d879795e authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

Revert "REST CRUD Methoden"

This reverts commit 40ed1054.
parent e513b6b2
......@@ -48,12 +48,12 @@
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support-->
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<dependency>
<groupId>javax.xml.bind</groupId>
......@@ -75,7 +75,6 @@
<artifactId>jaxb-impl</artifactId>
<version>2.2-promoted-b65</version>
</dependency>
</dependencies>
......
package university.at.jku.ce;
import java.util.HashMap;
import java.util.Map;
public class Database {
private static Map<Long ,Student> students=new HashMap<Long,Student>();
private static Map<Long ,Subject> subjects=new HashMap<Long,Subject>();
public static Map<Long, Student> getStudents() {
return students;
}
public static Map<Long, Subject> getSubjects() {
return subjects;
}
}
package university.at.jku.ce;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
......@@ -10,19 +10,13 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private long matrNr;
private int matrNr;
private String firstName;
private String lastName;
List <Subject> subjects;
public Student () {};
public Student(String firstName, String lastName, List<Subject> subjects) {
this.firstName=firstName;
this.lastName=lastName;
this.subjects=subjects;
}
public Student(int matrNr, String firstName, String lastName, List<Subject> subjects) {
this.matrNr=matrNr;
this.firstName=firstName;
......@@ -46,11 +40,11 @@ public class Student {
this.lastName = lastName;
}
public long getMatrNr() {
public int getMatrNr() {
return matrNr;
}
public void setMatrNr(long matrNr) {
public void setMatrNr(int matrNr) {
this.matrNr = matrNr;
}
......@@ -61,4 +55,9 @@ public class Student {
public void setSubjects(List<Subject> subjects) {
this.subjects = subjects;
}
}
package university.at.jku.ce;
import javax.ws.rs.core.GenericEntity;
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.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/students")
/**
* Root resource (exposed at "myresource" path)
*/
@Path("/students")
public class StudentResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
StudentService studentService=new StudentService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getStudents() {
List<Student>list= studentService.getAllStudents();
GenericEntity<List<Student>> entity=new GenericEntity<List<Student>>(list) {};
return Response.ok(entity).build();
}
// @GET
// @Produces(MediaType.APPLICATION_XML)
// public List <Student> getStudents() {
// return studentService.getAllStudents();
// }
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student addStudent(Student student) {
return studentService.addStudent(student);
}
// @POST
// @Consumes(MediaType.APPLICATION_XML)
// @Produces(MediaType.APPLICATION_XML)
// public Student addMessage(Student student) {
// return studentService.addStudent(student);
// }
@GET
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
return studentService.getStudent(matrNr);
}
@Produces(MediaType.APPLICATION_XML)
public List <Student> getStudents() {
System.out.println("Start: getStudents");
return studentService.getAllStudents();
}
// @GET
// @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML)
// public Student getStudent(@PathParam("studentMatrNr")long matrNr) {
// return studentService.getStudent(matrNr);
// }
@PUT
@Path("/{studentMatrNr}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) {
student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
return studentService.updateStudent(student);
}
// @PUT
// @Path("/{studentMatrNr}")
// @Consumes(MediaType.APPLICATION_XML)
// @Produces(MediaType.APPLICATION_XML)
// public Student updateStudent(@PathParam("studentMatrNr")long matrNr,Student student) {
// student.setMatrNr(matrNr); //if User does not send correct id it is still used correct id from URL
// return studentService.updateStudent(student);
// }
@DELETE
@Path("/{studentMatrNr}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteStudent(@PathParam("studentMatrNr")long matrNr) {
studentService.removeStudent(matrNr);
}
// @DELETE
// @Path("/{studentMatrNr}")
// @Produces(MediaType.APPLICATION_XML)
// public void deleteStudent(@PathParam("studentMatrNr")long matrNr) {
// studentService.removeStudent(matrNr);
// }
}
......@@ -2,58 +2,24 @@ package university.at.jku.ce;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class StudentService {
private Map <Long, Student> students=Database.getStudents();
//private Map <Long,Subject> subjects=Database.getSubjects();
public StudentService() {
List<Subject> subjects1=new ArrayList<Subject>();
subjects1.add(new Subject("Softwareenwicklung", "Grundlegendes zu Java"));
subjects1.add(new Subject("Datenmodellierung","Grundlegendes zu SQL und Relationalen Datenbanken"));
subjects1.add(new Subject("Communications Engineering","Grundlegendes Verteilten Systemen"));
List<Subject>subjects2=new ArrayList<Subject>();
subjects2.add(new Subject("Softwareenwicklung2", "Weiterführendes zu Java"));
subjects2.add(new Subject("DKE","Weiterführendes zu Data and Knowledge Engineering"));
Student s2=new Student(1, "Michael", "Schimpelsberger", subjects1);
Student s1=new Student(2,"Franz", "Reschl", null);
Student s3=new Student(3,"Susanne", "Zaunmayr", subjects2);
students.put(s1.getMatrNr(),s1);
students.put(s2.getMatrNr(),s2);
students.put(s3.getMatrNr(), s3);
}
public List<Student> getAllStudents(){
return new ArrayList<Student>(students.values());
}
public Student getStudent(Long id) {
return students.get(id);
}
public Student addStudent(Student student) {
if (student!=null) {
student.setMatrNr(students.size()+1);
students.put(student.getMatrNr(), student);
}
return student;
}
public Student updateStudent(Student student) {
if (student!=null&&students.containsKey(student.getMatrNr()));
students.put(student.getMatrNr(), student);
return student;
List<Subject> subjects=new ArrayList<Subject>();
subjects.add(new Subject("Softwareenwicklung", "Grundlegendes zu Java"));
subjects.add(new Subject("Datenmodellierung","Grundlegendes zu SQL und Relationalen Datenbanken"));
Student s2=new Student(1156101, "Michael", "Schimpelsberger", subjects);
Student s1=new Student(1234567,"Franz", "Reschl", null);
Student s3=new Student(2424247,"Susanne", "Zaunmayr", subjects);
List<Student> students=new ArrayList<Student>();
students.add(s1);
students.add(s2);
students.add(s3);
return students;
}
public Student removeStudent(long matrNr) {
return students.remove(matrNr);
}
}
package university.at.jku.ce;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String profession;
public User(){}
public User(int id, String name, String profession){
this.id = id;
this.name = name;
this.profession = profession;
}
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getProfession() {
return profession;
}
@XmlElement
public void setProfession(String profession) {
this.profession = profession;
}
}
\ No newline at end of file
package university.at.jku.ce;
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<User> getAllUsers(){
List<User> userList = null;
try {
File file = new File("Users.dat");
if (!file.exists()) {
User user = new User(1, "Mahesh", "Teacher");
userList = new ArrayList<User>();
userList.add(user);
saveUserList(userList);
}
else{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
userList = (List<User>) ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return userList;
}
private void saveUserList(List<User> 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 newline at end of file
package university.at.jku.ce;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment