POST在RESTful客户端通信中返回响应状态500 Internal Server Error [英] POST returned a response status of 500 Internal Server Error in RESTful client communication

查看:3053
本文介绍了POST在RESTful客户端通信中返回响应状态500 Internal Server Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,

我正在使用Java中的RESTful Web服务,它在从Java应用程序进行通信时会导致500错误

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

我参考了本教程来做同样的事情 在Java中实现RESTful Web服务

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

这是RESTful网络服务

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;
    }

}

Web服务客户端到Java应用程序

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);
        }
    }
}

例外

ex =(com.sun.jersey.api.client.UniformInterfaceException) com.sun.jersey.api.client.UniformInterfaceException:POST http://localhost:8050/WebRestfulAppTest/services/WebTest/add 返回 响应状态为500 Internal Server Error

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

推荐答案

在Web服务中存在异常add:

there is an Exception in the web services add :

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

这篇关于POST在RESTful客户端通信中返回响应状态500 Internal Server Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆