这不是重复的问题。我当前的设置没有像这里的许多问题一样使用web.xml,也没有使用maven或gradle,所以我没有缺少依赖项。我定义了我的ApplicationPath(“ test”)和我的Path(“ Service”)。这两个类都位于同一程序包中:网站。我尝试使用ResourceConfig而不是扩展Application,但这还是行不通的
@ApplicationPath("/test")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet h = new HashSet<Class<?>>();
h.add(Service.class);
return h;
}
也尝试过
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(Service.class)
packages("website");
}
}
服务等级
@Path("Service")
public class Service {
FormResource formResource = new FormResource();
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void setForm(@FormParam("doctor") String doctor, @FormParam("message") String message, @Context HttpServletRequest request, @Context HttpServletResponse response) throws ServletException, IOException {
Form form = new Form();
//with post parameters
form.setDoctor(doctor);
form.setMessage(message);
formResource.setForm(form);
request.getRequestDispatcher("WEB-INF/form.jsp").forward(request, response);
}
@GET
public void redirect(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException, ServletException
{
request.getRequestDispatcher("WEB-INF/form.jsp").forward(request, response);
}
}
这些都不起作用。我正在尝试通过localhost:8080 / mywar / test / Service访问它,但收到404错误。任何帮助将不胜感激