问题描述
我是休息网络服务的新手。我想用 wildfly 20 测试教程 https://www.baeldung.com/resteasy-tutorial。我用从 github 获得的代码创建了一个 maven 项目。我构建了该项目并成功部署了它。
但如果我尝试通过邮递员(即 http://127.0.0.1:8080/resteasy/movies/listmovies)进行休息调用,我会收到“错误 404 未找到”错误。
这里是 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>
这里是 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));
}
这里是 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();
}
}
电影.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;
}
...
我做错了什么? 非常感谢,妮可
解决方法
您的 servlet 映射是:
<param-value>/rest</param-value>
因此,您必须调用的 url 应在 webapp 上下文路径 (/resteasy) 和资源路径 (/movies/listmovies) 之间包含“/rest”: