RestEasy Web 服务服务器教程 Baeldung 无法正常工作 Wildfly 20

RestEasy Webservice Server Tutorial Baeldung not working Wildfly 20

我是休息网络服务的新手。我想用 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();
    }
}

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

我做错了什么? 非常感谢,妮可

您的 servlet 映射是:

<param-value>/rest</param-value>

所以您必须调用的 url 应该在 webapp 上下文路径 (/resteasy) 和资源路径 (/movies/listmovies) 之间包含“/rest”:

http://127.0.0.1:8080/resteasy/rest/movies/listmovies