带有Bootstrap / REST的Jersey的web.xml配置/结构

时间:2018-08-25 13:00:30

标签: java html rest jersey glassfish

我使用以下技术:Glassfish,泽西岛 我在IntelliJ中获得了以下项目结构:

beat_api
  ->.idea
  ->lib
  ->out
  ->src
     ->ApplicationController.java
     ->Beat.java
  ->web
     ->WEB-INF
       web.xml
     ->index.jsp (want to convert that to HTML though, no idea how .jsp works)

我有以下问题:

  1. 我的结构正确吗?我在哪里放置.html / .css文件进行连接 到我的REST API?完成基本标记后,我想添加Bootstrap以使其看起来不错。
  2. 如何为我的项目结构配置web.xml?我不明白,无论我尝试什么,都无法通过转到localhost:8080 / beat_api /
  3. 从我的项目结构中显示index.jsp。
  4. 我希望能够创建“ Beat”并搜索 “节拍”,最好在JSON或XML文件中(使用SAX解析器)。如何在Beat.java资源中创建Beat实例?每当我在其中放置构造函数时,都会收到以下错误消息:“ java.lang.NoSuchMethodException:在Beat类中找不到合适的构造函数。”

Beat.java

import javax.ws.rs.*;
// The Java class will be hosted at the URI path "/beats"

@Path("/beats")
public class Beat {

    // The Java method will process HTTP GET requests
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @GET
    @Produces("text/plain")
    @Path("/show")
    public String retrieveBeat() {
        return "hello";
    }

    // The Java method will process HTTP GET requests
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @POST
    @Consumes("text/plain")
    @Path("create/{beatname}/{beatbpm}/{beatkey}")
    public void createBeat(@PathParam("beatname") String beatname,
                           @PathParam("beatbpm") int beatbpm,
                           @PathParam("beatkey") String beatkey) {
    }
}

ApplicationController.java

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

//Defines the base URI for all resource URIs.
@ApplicationPath("/")
//The java class declares root resource and provider classes
public class ApplicationController extends Application{
    //The method returns a non-empty collection with classes, 
    // that must be included in the published JAX-RS application
    @Override
    public Set<Class<?>> getClasses() {
        HashSet h = new HashSet<Class<?>>();
        h.add( Beat.class );
        return h;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

0 个答案:

没有答案