POST returned a response status of 500 Internal Se

2019-09-01 06:03发布

问题:

Folks,

I am working with the RESTful webservice in java and it causing the 500 error while communicating from the Java application

I referred this tutorial to do the same implementing RESTful Web Services in Java

here is the RESTful webservice

@Path("WebTest")
@Produces("application/json")

public class WebTestResource {

    private TreeMap<Integer, BookDemo> bookMap = new TreeMap<Integer, BookDemo>();

    public WebTestResource() {
    }


    @GET
    public List<BookDemo> getBooks() {
        List<BookDemo> books = new ArrayList<BookDemo>();
        books.addAll(bookMap.values());
        return books;
    }

    @GET
    @Path("{id}")
    public BookDemo getBook(@PathParam("id") int bookId) {
        return bookMap.get(bookId);
    }

    @POST
    @Path("add")
    @Produces("text/plain")
    @Consumes("application/json")
    public String addBook(BookDemo book) {
        int id = bookMap.size();
        try{

            book.setId(id);
            bookMap.put(id, book);


            String strFilePath = "D:\\Java™\\RESTful\\test.txt";
            FileWriter file= null;
            try {
                file = new FileWriter(strFilePath);
                file.write(book.getBookName());
            } catch (IOException ex) {
                Logger.getLogger(WebTestResource.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally{
                try {
                    file.flush();
                    file.close();
                } catch (IOException ex) {
                    Logger.getLogger(WebTestResource.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }
        catch(Exception ex){

        }


        return "Book \"" + book.getBookName() + "\" added with Id " + id;
    }

}

Webservice Client to the java application

public class Restful {
    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:8050/WebRestfulAppTest/services";

    public Restful() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("WebTest");
    }

    public <T> T getBook(Class<T> responseType, String id) throws UniformInterfaceException {
        WebResource resource = webResource;
        resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
        return resource.get(responseType);
    }

    public String addBook(Object requestEntity) throws UniformInterfaceException {
        return webResource.path("add").type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(String.class, requestEntity);
    }

    public <T> T getBooks(Class<T> responseType) throws UniformInterfaceException {
        WebResource resource = webResource;
        return resource.get(responseType);
    }

    public void close() {
        client.destroy();
    }

}

POJO

@XmlRootElement(name = "BookDemo")
public class BookDemo {
private int id;

private String bookName;

private String bookAuthor;

private String bookISBN;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}


public String getBookAuthor() {
return bookAuthor;
}

public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}

public String getBookISBN() {
return bookISBN;
}

public void setBookISBN(String bookISBN) {
this.bookISBN = bookISBN;
}


}

main()

public class GetCallService {
    static Restful objRestful = new Restful();


    public static void main(String[] args){
        try{
        BookDemo book = new BookDemo();
        book.setBookAuthor("ABC");
        book.setBookName("Introduction to RESTful Web Services Example test application");
        book.setBookISBN("ISBN 10: 0-596-52926-0");
        objRestful.addBook(book);
        BookDemo book1 = new BookDemo();
        book1.setBookAuthor("XYZ");
        book1.setBookName("RESTfull web");
        book1.setBookISBN("ISBN 10: 0-596-52926-0 Prajwal");
        objRestful.addBook(book1);
        }
        catch(Exception ex){
            ErrorLog.errorLog(ex);
        }
    }
}

Exception

ex = (com.sun.jersey.api.client.UniformInterfaceException) com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:8050/WebRestfulAppTest/services/WebTest/add returned a response status of 500 Internal Server Error

回答1:

there is an Exception in the web services add :

catch(Exception ex){
    ex.printStackTrace();
}