Created
September 29, 2015 13:22
-
-
Save barais/25e7efc8e6c69f45a21b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package fr.ensai.jaxrs; | |
| import java.text.DateFormat; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.List; | |
| import javax.swing.text.DateFormatter; | |
| import javax.ws.rs.Consumes; | |
| import javax.ws.rs.GET; | |
| import javax.ws.rs.POST; | |
| import javax.ws.rs.Path; | |
| import javax.ws.rs.Produces; | |
| import javax.ws.rs.core.MediaType; | |
| import fr.ensai.jaxrs.domain.Message; | |
| @Path("/messages") | |
| public class MessageServiceImpl implements MessageService { | |
| static List<Message> messages = new ArrayList<Message>(); | |
| public MessageServiceImpl() { | |
| Message m1 = new Message(); | |
| m1.setName("foo"); | |
| m1.setDate(new Date()); | |
| m1.setContent("Hello"); | |
| this.addMessage(m1); | |
| } | |
| @Override | |
| @GET | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public List<Message> getAllMessages() { | |
| return messages; | |
| } | |
| @GET | |
| //@Path("/all") | |
| @Produces(MediaType.TEXT_HTML) | |
| public String getAllMessagesinHTML() { | |
| String s = "<html><body>"; | |
| for (Message m : this.messages){ | |
| s = s + "--------------------------"+"<BR>"; | |
| s = s + "Name:" + m.getName() +"<BR>" ; | |
| s = s + "Date:" + m.getDate().toLocaleString()+"<BR>"; | |
| s = s + "Content:" + m.getContent() +"<BR>"; | |
| s = s + "--------------------------"; | |
| } | |
| s =s + "</body></html>"; | |
| return s; | |
| } | |
| @POST | |
| @Consumes(MediaType.APPLICATION_JSON) | |
| @Override | |
| public void addMessage(Message m) { | |
| messages.add(m); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment