JerseyServletModule和ServletModule有什么区别?

时间:2018-08-06 17:22:58

标签: jersey guice guice-servlet

我试图了解// Recursively create a tree of elements with attributes and children const partition = xs => xs.reduce( ([lastPart, ...parts], x) => { switch(getType(x)) { case Element: return [ Element(x), ...(lastPart ? [lastPart] : []), ...parts ]; case Array: const attrsUntil = x.findIndex(Attribute.isNoAttribute); const attrs = attrsUntil !== -1 ? x.slice(0, attrsUntil) : x; const children = attrsUntil === -1 ? [] : x.slice(attrsUntil); return [ Element( lastPart.tagName, attrs.map(Attribute.fromString), partition(children) ), ...parts]; default: return [ lastPart, ...parts ]; } }, [] ).reverse() const getType = x => Array.isArray(x) ? Array : Attribute.isAttribute(x) ? Attribute : Element; // Some utility models & methods: const Attribute = (key, value) => ({ key, value }); Attribute.isAttribute = (str) => str.charAt && str.charAt(0) === "#"; Attribute.isNoAttribute = x => !Attribute.isAttribute(x); Attribute.fromString = str => Attribute( ...str.split("#").slice(1) ); const Element = (tagName, attributes = [], children = []) => ({ tagName, attributes, children }); let example_arr = [ "backend", [ "#host#www.example.com", "#port#80" ], "endpoints", [ "endpoint", [ "#external#/foo/bar/cat/mom/", "#internal#/banana/", "params", [ "param", [ "#sourceValue#acis.usermove.md" ] ] ], ] ]; console.log( partition(example_arr) );库中的// The formatted tree: const input = [{tagName:"backend",attributes:[{key:"host",value:"www.example.com"},{key:"port",value:"80"}],children:[]},{tagName:"endpoints",attributes:[],children:[{tagName:"endpoint",attributes:[{key:"external",value:"/foo/bar/cat/mom/"},{key:"internal",value:"/banana/"}],children:[{tagName:"params",attributes:[],children:[{tagName:"param",attributes:[{key:"sourceValue",value:"acis.usermove.md"}],children:[]}]}]}]}]; // A helper to prefix with an indentation const indented = (n, str) => `${" ".repeat(n)}${str}`; // This renders a self closing tag const renderEmptyEl = (el, indent) => indented( indent, `<${el.tagName} ${renderAttributes(el)} />` ); // This renders a tag that has children const renderParentEl = (el, indent) => [ indented(indent, `<${el.tagName} ${renderAttributes(el)}>`), ...el.children.map(c => renderEl(c, indent + 2)), indented(indent, `</${el.tagName}>`) ].join("\n"); const renderEl = (el, indent) => el.children.length ? renderParentEl(el, indent) : renderEmptyEl(el, indent); // Render the attribute values const renderAttributes = ({ attributes }) => attributes .map(({ key, value }) => `${key}="${value}"`) .join(" "); // Entry point for an array of nodes not inside another Element const renderTree = (nodes) => nodes.map(n => renderEl(n, 0)).join("\n"); console.log(renderTree(input));JerseyServletModule库中的jersey-guice之间的区别。

1 个答案:

答案 0 :(得分:0)

TL; DR-JerseyServletModule已死。过去(在泽西岛为期1天)允许您使用泽西岛和吉斯,并让他们发挥出色。这不再简单了。

ServletModule是Guice模块,它允许您使用Guice配置servlet,servlet映射,过滤器和过滤器映射(以及其他功能),以及将对象注入到servlet中。创建一个从ServletModule继承的类,并重写configureServlets方法,如下所示:

class MyModule extends ServletModule {

    @Override
    protected void configureServlets() {
        serve("/hello").with(HelloAppEngine.class);
    }
}

使用Guice和ServletModule构建Web服务的文档为here

Jersey是JSR 370的实现,即所谓的JAX-RS API,用于创建休息服务。它允许您仅使用带注释的类来构建rest服务-例如:

@Path("/user")
public class UserController {

    UserPersistService userPersistService;

    ObjectMapper objectMapper;

    UserController(UserPersistService userPersistService, ObjectMapper objectMapper) {
        this.userPersistService = userPersistService;
        this.objectMapper = objectMapper;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getUsersAsJSON() throws IOException {
        final List<User> users = userPersistService.getUsers();
        return objectMapper.writeValueAsString(users);
    }
}

在泽西岛1.0的过去,重写JerseyServletModule而不是Servlet Module可以使您同时执行这两个操作,因此上述类可能变为:

@Singleton
@Path("/user")
public class UserController {

    @Inject
    UserPersistService userPersistService;

    @Inject
    ObjectMapper objectMapper;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getUsersAsJSON() throws IOException {
        final List<User> users = userPersistService.getUsers();
        return objectMapper.writeValueAsString(users);
    }

但是,正如我在开头提到的那样,在Jersey 2.0中没有等效的JerseyServletModule。您的选择是配置HK-Guice桥并继续使用Jersey,或使用RestEasy(这是替代JAX-RS实现,可以很好地与guice配合使用)。