Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This one is complex and I don\'t know how to do it. One has to configure Restful

ID: 3549774 • Letter: T

Question


This one is complex and I don't know how to do it. One has to configure Restful web Services Java


Write a restful web service demo example using spring mvc.

Here are the steps to be followed.

1. Make a map for a book with Book ISBN as key and book title as value


2. Write a method to get list of all the books in JSON format using restul client.


3. Write a method to get a particular book from the map in JSON format.


4. Write a method to add a book in the map. Data to be entered in JSON format


5. Write a method to delete a book from the map


6.Write a method to add multiple books in the map.


Use jackson mapper for java to json conversion.


Explanation / Answer



import java.io.File;

import java.io.IOException;

import java.io.Writer;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import javax.servlet.http.HttpServletRequest;




import org.codehaus.jackson.JsonFactory;

import org.codehaus.jackson.JsonGenerationException;

import org.codehaus.jackson.map.JsonMappingException;

import org.codehaus.jackson.map.ObjectMapper;

import org.codehaus.jackson.map.type.TypeFactory;

import org.codehaus.jackson.type.TypeReference;

import org.springframework.http.HttpStatus;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.ResponseStatus;


import com.tcs.bean.Book;




@Controller

@RequestMapping("/restful")

@ResponseStatus(value = HttpStatus.ACCEPTED)

public class RestfulController {


static Map<String, Book> books = new HashMap<String, Book>();


public Map<String, Book> addBooks()

{

books.put("1", new Book("1","Let Us See"));

books.put("2", new Book("2","Let Us not See"));

books.put("3", new Book("3","Why dont you See"));

books.put("4", new Book("4","Ok. ur wish. dont See"));

books.put("5", new Book("5","hurrey"));

return books;

}

@RequestMapping(value = "/books", method = RequestMethod.GET)

public String getBooks( ModelMap model) throws JsonGenerationException, JsonMappingException, IOException {


RestfulController rest = new RestfulController();

rest.addBooks();

  

ObjectMapper mapper = new ObjectMapper();

String json = mapper.writeValueAsString(books);

System.out.println(mapper.writeValueAsString(books) );

model.addAttribute("message", json);

return "list";

}

@RequestMapping(value = "/books/{isbn}", method = RequestMethod.GET)

public String getBook(@PathVariable String isbn, ModelMap model) throws Exception {


RestfulController rest = new RestfulController();

rest.addBooks();

  

ObjectMapper mapper = new ObjectMapper();

String json = null;

  

if (books.containsKey(isbn))

{

  

json = mapper.writeValueAsString(books.get(isbn));

  

}

else

{

throw new Exception(); //book not found

}


  

  

model.addAttribute("message", json);

return "list";

}




@RequestMapping(value = "/books/{isbn}", method = RequestMethod.DELETE)

public String deleteLink(@PathVariable("isbn") String isbn, ModelMap model) throws Exception {

RestfulController rest = new RestfulController();

rest.addBooks();

  

ObjectMapper mapper = new ObjectMapper();

String json = null;

if (books.containsKey(isbn))

{

books.remove(isbn);

  

json = mapper.writeValueAsString(books);

System.out.println("json = " + json);

}

else

{

throw new Exception();

}

  

model.addAttribute("message", json);

return "list";

}

@RequestMapping(value = "/books/{isbn}/{title}", method = RequestMethod.POST)

public String postLink(@PathVariable("isbn") String isbn,@PathVariable ("title")String title, ModelMap model) throws Exception {

RestfulController rest = new RestfulController();

rest.addBooks();

books.put(isbn, new Book(isbn,title));

ObjectMapper mapper = new ObjectMapper();

String json = mapper.writeValueAsString(books);

System.out.println(mapper.writeValueAsString(books) );

model.addAttribute("message", json);

return "list";


}

@RequestMapping(value = "/books/{isbn}", method = RequestMethod.POST)

public String postLink2( @RequestBody String body, Writer writer, ModelMap model) throws Exception {

RestfulController rest = new RestfulController();

rest.addBooks();

writer.write(body);

System.out.println(body);

// json to map

JsonFactory factory = new JsonFactory();

ObjectMapper mapper = new ObjectMapper(factory);

// File from = new File(body);

TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>()

{

};

HashMap<String,Book>books2 = mapper.readValue(body, typeRef);

System.out.println("Got " + books2);

books.putAll(books2);

System.out.println("first Map "+books);

System.out.println("second Map "+books2);

//map to json again

String json = mapper.writeValueAsString(books);

System.out.println(mapper.writeValueAsString(books) );

model.addAttribute("message",json );

return "list";


}


@RequestMapping(value = "/books", method = RequestMethod.POST)

public String postLink3( @RequestBody String body, Writer writer, ModelMap model) throws Exception {

RestfulController rest = new RestfulController();

rest.addBooks();

writer.write(body);

System.out.println(body);

// json to map

JsonFactory factory = new JsonFactory();

ObjectMapper mapper = new ObjectMapper(factory);

// File from = new File(body);

TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>()

{

};

HashMap<String,Book>books2 = mapper.readValue(body, typeRef);

System.out.println("Got " + books2);

books.putAll(books2);

System.out.println("first Map "+books);

System.out.println("second Map "+books2);

//map to json again

String json = mapper.writeValueAsString(books);

System.out.println(mapper.writeValueAsString(books) );

model.addAttribute("message",json );

return "list";


}

@ExceptionHandler(ResourceNotFoundException.class)

@ResponseStatus(value = HttpStatus.BAD_GATEWAY)

public void exceptions(ResourceNotFoundException ex, HttpServletRequest request) {

System.out.println("handleNotFoundException:" + ex);

}

  

}


@SuppressWarnings("serial")

class ResourceNotFoundException extends RuntimeException {

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote