嗨有标题说我有几个问题。我的客户端不断出现错误500,但不确定服务器中出现错误的是什么。我认为这可能是通过的参数,但我不确定。我在客户端也有注释代码的问题,但我认为这是参数特别但现在我遇到了一个问题(在客户端的主体中没有注释的代码)所以现在我不知道在哪里继续从这里。我想这可能是路径,但我想我只需要一双新的眼睛来指出我犯了什么愚蠢的错误。请告诉我你的想法和答案如下。谢谢!
客户端
public class Tester {
private static URI getBaseURI(){
return UriBuilder.fromUri("http://localhost:8080/Assignment-tomcat_with_custom_api").build();
}
public static void main(String[] args){
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
/*
//example of creation
Film film = new Film(50000000, "film1", 1155, "film1", "film1", "film1");
ClientResponse response = service.path("rest").path("films").path(""+film.getId()).accept(MediaType.APPLICATION_XML).put(ClientResponse.class, film);
if(response.getStatus() == 201){
System.out.println("Film has been created");
}else{
System.out.println("Film had failed to be created. Response status:"+response.getStatus());
}
*/
System.out.println();
//get all films
System.out.println(service.path("rest").path("films").accept(
MediaType.TEXT_XML).get(String.class));
}
}
服务器
@Path("/films")
public class FilmListResource {
@Context UriInfo uriInfo;
@Context Request request;
//returns all films to browser
@GET
@Produces(MediaType.TEXT_XML)
public ArrayList<Film> getFilmListBrowser(){
FilmDAO dao = new FilmDAO();
return dao.getAllFilms();
}
//list of film applications
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public ArrayList<Film> getFilmList(){
FilmDAO dao = new FilmDAO();
return dao.getAllFilms();
}
//return number of films
@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String getCount(){
FilmDAO dao = new FilmDAO();
return String.valueOf(dao.getAllFilms().size());
}
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void newFilm(@FormParam("title") String title,
@FormParam("director") String director,
@FormParam("year") int year,
@FormParam("stars") String stars,
@FormParam("review") String review,
@Context HttpServletResponse servletResponse)
throws IOException{
FilmDAO dao = new FilmDAO();
ArrayList<Film> films = dao.getAllFilms();
int id = films.get(films.size()-1).getId()+1;
Film film = new Film(id, title, year, director, stars, review);
dao.addFilm(film);
servletResponse.sendRedirect(".../create_film.html");
}
/*
* allows id to be used as a parameter to retrieve a film
*/
@Path("{Film}")
public FilmResource getFilm (@PathParam("Film") String id){
System.out.println("milestone 1");
return new FilmResource(uriInfo, request, id);
}
}
public class FilmResource {
@Context UriInfo uriInfo;
@Context Request request;
String id;
public FilmResource(UriInfo uriInfo, Request request, String id){
this.uriInfo = uriInfo;
this.request = request;
this.id = id;
}
//application integration
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Film getFilm(){
FilmDAO dao = new FilmDAO();
int film_id = Integer.valueOf(id);
Film film = dao.getFilmByID(film_id);
if(film == null){
throw new RuntimeException("Film could not be found using id:"+film_id);
}
return film;
}
//make browser pages
@GET
@Produces(MediaType.TEXT_XML)
public Film getFilmHtml(){
FilmDAO dao = new FilmDAO();
int film_id = Integer.valueOf(id);
Film film = dao.getFilmByID(film_id);
if(film == null){
throw new RuntimeException("Film could not be found using id:"+film_id);
}
return film;
}
@PUT
@Consumes(MediaType.APPLICATION_XML)
public Response putFilm(JAXBElement<Film> JaxFilm){
Response res;
Film film = JaxFilm.getValue();
FilmDAO dao = new FilmDAO();
if(dao.getFilmByID(film.getId()) != null){
res = Response.noContent().build();
}else{
res = Response.created(uriInfo.getAbsolutePath()).build();
}
dao.addFilm(film);
return res;
}
}