将Elasticsearch与jetty jersey搭配使用

时间:2018-04-24 15:17:38

标签: elasticsearch jersey jetty

我正在使用弹性搜索,它运行良好,但是当我尝试将它与带有码头和球衣的网络服务一起使用时。

以下是我想要使用的函数示例:

public boolean insertUser(RestHighLevelClient client, User user) throws IOException
    {
        java.util.Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("username", user.username);
        jsonMap.put("password", user.password);
        jsonMap.put("mail", user.mail);
        jsonMap.put("friends", user.friends);
        jsonMap.put("maps", user.maps);
        System.out.println("insertUser");
        IndexRequest indexRequest = new IndexRequest("users", "doc",user.username)
                .source(jsonMap);

        try {
            IndexResponse indexResponse = client.index(indexRequest);
            System.out.println("insertUser 222");
            if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                System.out.println("user "+user.username+" créé");
            }
            else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
                System.out.println("user "+user.username+" update dans insertUser (pas normal)");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

当我在测试类中尝试它时,此函数很有效。但如果我这样启动我的服务器:

Server server = new Server();

        // Add a connector
        ServerConnector connector = new ServerConnector(server);
        connector.setHost("0.0.0.0");
        connector.setPort(8081);
        connector.setIdleTimeout(30000);
        server.addConnector(connector);
        DAO.ClientConnection("0.0.0.0",8081);
        // Configure Jersey
        ResourceConfig rc = new ResourceConfig();
        rc.packages(true, "com.example.jetty_jersey.ws");
        rc.register(JacksonFeature.class);

        // Add a servlet handler for web services (/ws/*)
        ServletHolder servletHolder = new ServletHolder(new ServletContainer(rc));
        ServletContextHandler handlerWebServices = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handlerWebServices.setContextPath("/ws");
        handlerWebServices.addServlet(servletHolder, "/*");

        // Add a handler for resources (/*)
        ResourceHandler handlerPortal = new ResourceHandler();
        handlerPortal.setResourceBase("src/main/webapp/temporary-work");
        handlerPortal.setDirectoriesListed(false);
        handlerPortal.setWelcomeFiles(new String[] { "homepage.html" });
        ContextHandler handlerPortalCtx = new ContextHandler();
        handlerPortalCtx.setContextPath("/");
        handlerPortalCtx.setHandler(handlerPortal);

        // Activate handlers
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(new Handler[] { handlerWebServices, handlerPortalCtx });
        server.setHandler(contexts);

        // Start server
        server.start();

当我输入表单时,请调用此Web服务:

@POST
    @Path("/signup")
    @Produces(MediaType.APPLICATION_JSON)
    // @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public SimpleResponse signup(@Context HttpServletRequest httpRequest,
                                 @FormParam("username") String username,
                                 @FormParam("email") String email,
                                 @FormParam("password") String password,
                                 @FormParam("passwordConfirm") String passwordConfirm) {
        System.out.println("k");
        //if (httpRequest.getSession().getAttribute("user") != null) { //httpRequest.getUserPrincipal() == null) {
            try {
                if (password.equals(passwordConfirm)) {
                    User user = new User("jeanOknewmail@gmail.com", "abc");
                    user.username = "jeanok";
                    user.maps = new ArrayList<String>();
                    user.friends = new ArrayList<String>();
                    System.out.println(user);
                    System.out.println("avant insert");
                    DAO.getActionUser().createIndexUser();
                    //System.out.println(DAO.getActionUser().getOneUser(DAO.client, "joe"));
                    System.out.println("rdctfygbhunji,k");
                    DAO.getActionUser().insertUser(DAO.client, user);
                    System.out.println("après insert");
                    return new SimpleResponse(true);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        //}
        return new SimpleResponse(false);
    }

我收到很多错误:

avax.servlet.ServletException: ElasticsearchStatusException[Unable to parse response body]; nested: ResponseException[method [PUT], host [http://0.0.0.0:8081], URI [/users/doc/jeanok?timeout=1m], status line [HTTP/1.1 404 Not Found]|];
...
Caused by: 
ElasticsearchStatusException[Unable to parse response body]; nested: ResponseException[method [PUT], host [http://0.0.0.0:8081], URI [/users/doc/jeanok?timeout=1m], status line [HTTP/1.1 404 Not Found]|];
    at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:598)
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:501)
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:474)
    at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:335)
    at DAO.UserDAO.insertUser(UserDAO.java:160)

你知道为什么我启动服务器时我的功能行为不一样吗?为什么这个错误呢?谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我没有与弹性搜索相关联。我的客户端连接到错误的端口。现在它可以工作