民间,
我正在使用java中的RESTful webservice,它在从Java应用程序进行通信时导致500错误
我提到本教程也是这样做的 implementing RESTful Web Services in Java
这是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到java应用程序
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内部服务器错误
答案 0 :(得分:0)
Web服务添加中存在异常:
catch(Exception ex){
ex.printStackTrace();
}