I am new to rest webservices. I wanted to test the tutorial https://www.baeldung.com/resteasy-tutorial with wildfly 20. I created a maven project with the code I got from github. I built the project and sucessfully deployed it.
But if I try to make rest calls via postman (i.e. http://127.0.0.1:8080/resteasy/movies/listmovies) I get "Error 404 not found" errors.
Here is the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>resteasy</display-name>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
Here is the MovieCrudService.java
@Path("/movies")
public class MovieCrudService {
private Map<String, Movie> inventory = new HashMap<String, Movie>();
@GET
@Path("/")
@Produces({ MediaType.TEXT_PLAIN })
public Response index() {
return Response.status(200).header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD").entity("").build();
}
...
@GET
@Path("/listmovies")
@Produces({ "application/json" })
public List<Movie> listMovies() {
return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new));
}
Here is RestEasyServices.java
@ApplicationPath("/rest")
public class RestEasyServices extends Application {
private Set<Object> singletons = new HashSet<Object>();
public RestEasyServices() {
singletons.add(new MovieCrudService());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
@Override
public Set<Class<?>> getClasses() {
return super.getClasses();
}
@Override
public Map<String, Object> getProperties() {
return super.getProperties();
}
}
Movie.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "movie", propOrder = { "imdbId", "title" })
public class Movie {
protected String imdbId;
protected String title;
public Movie(String imdbId, String title) {
this.imdbId = imdbId;
this.title = title;
}
public Movie() {}
public String getImdbId() {
return imdbId;
}
...
What am I doing wrong? Thanks a lot, Nicole
Your servlet mapping is:
<param-value>/rest</param-value>
So the url you have to call should include "/rest" between the webapp context-path (/resteasy) and the resource path (/movies/listmovies):