Vaadin应用程序中的Web服务

时间:2014-08-22 16:26:10

标签: web-services vaadin

我有一个Vaadin应用程序,我添加了Web服务,现在只有Web服务似乎在Web应用程序上运行。我在服务器上看到了Vaadin活动,但它的UI从未出现,只是一条关于只有可用的Web服务的消息。

“目前只提供通过网络服务提供的功能。”

可在此处找到来源:https://javydreamercsw@bitbucket.org/javydreamercsw/validation-manager

更新: 我尝试了对网络服务的评价,但仍然有同样的效果。以下是请求的web.xml文件内容:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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_2_5.xsd">
    <display-name>Vaadin Web Application</display-name>
    <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
    </context-param>
    <!--Comment if not using H2 database-->
    <context-param>
        <param-name>db.url</param-name>
        <param-value>jdbc:h2:file:~/VM/data/validation-manager;CREATE=TRUE;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE</param-value>
    </context-param>
    <context-param>
        <param-name>db.user</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>db.password</param-name>
        <param-value/>
    </context-param>
    <listener>
        <listener-class>org.h2.server.web.DbStarter</listener-class>
    </listener>
    <!--End of H2 database exclusive settings-->
    <filter>
        <description>Make GB2312\GBK or others to UTF-8 for chineese character support</description>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>net.sourceforge.javydreamercsw.validation.manager.web.filter.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
            <description>Vaadin UI to display</description>
            <param-name>UI</param-name>
            <param-value>com.validation.manager.ValidationManagerUI</param-value>
        </init-param>
        <init-param>
            <description>Application widgetset</description>
            <param-name>widgetset</param-name>
            <param-value>com.validation.manager.AppWidgetSet</param-value>
        </init-param>
    </servlet>
    <servlet>
        <servlet-name>ValidationManagerService</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ValidationManagerService</servlet-name>
        <url-pattern>/ValidationManagerService</url-pattern>
    </servlet-mapping>
</web-app>

Web服务注释:

package net.sourceforge.javydreamercsw.validation.manager.web.service;

import com.validation.manager.core.DataBaseManager;
import com.validation.manager.core.VMException;
import com.validation.manager.core.db.Project;
import com.validation.manager.core.db.VmUser;
import com.validation.manager.core.db.controller.ProjectJpaController;
import com.validation.manager.core.server.core.ProjectServer;
import com.validation.manager.core.tool.MD5;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.openide.util.Exceptions;

/**
 *
 * @author Javier A. Ortiz Bultron <javier.ortiz.78@gmail.com>
 */
@WebService(serviceName = "VMWebService")
public class VMWebService {

    private Map<String, Object> parameters = new HashMap<>();

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getVMUser")
    public VmUser getVMUser(@WebParam(name = "user") final String username,
            @WebParam(name = "pass") final String password) throws VMException {
        VmUser user = isValidUser(username, password);
        if (user == null) {
            throw new VMException("Invalid username and/or password!");
        }
        return user;
    }

    private VmUser isValidUser(String username, String password) {
        VmUser user = null;
        parameters.clear();
        parameters.put("username", username);
        List<Object> result
                = DataBaseManager.namedQuery("VmUser.findByUsername",
                        parameters);
        if (result != null) {
            for (Object obj : result) {
                if (obj instanceof VmUser) {
                    try {
                        VmUser vmuser = (VmUser) obj;
                        if (vmuser.getPassword().equals(MD5.encrypt(password))) {
                            user = vmuser;
                        }
                    } catch (Exception ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            }
        }
        return user;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getProject")
    public Project getProject(@WebParam(name = "project") final Project project,
            @WebParam(name = "user") final VmUser user) throws VMException {
        Project p = null;
        if (isValidUser(user.getUsername(), user.getPassword()) != null) {
            p = new ProjectServer(project).getEntity();
        }
        return p;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getRootProjects")
    public List<Project> getRootProjects(@WebParam(name = "user") final VmUser user)
            throws VMException {
        List<Project> projects = new ArrayList<>();
        if (isValidUser(user.getUsername(), user.getPassword()) != null) {
            ProjectJpaController controller
                    = new ProjectJpaController(DataBaseManager.getEntityManagerFactory());
            for (Project p : controller.findProjectEntities()) {
                if (p.getParentProjectId() == null) {
                    projects.add(p);
                }
            }
        }
        return projects;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "saveProject")
    public boolean saveProject(@WebParam(name = "newProject") final Project newProject,
            @WebParam(name = "user") final VmUser user) throws VMException {
        boolean result = false;
        if (isValidUser(user.getUsername(), user.getPassword()) != null) {
            ProjectServer ps = new ProjectServer(newProject);
            ps.copy(newProject);
            result = true;
        }
        return result;
    }
}

2 个答案:

答案 0 :(得分:0)

我们遇到了类似的问题:

<servlet-mapping>
    <servlet-name>Vaadin Application Servlet</servlet-name>
    <url-pattern>/admin/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>Vaadin Application Servlet</servlet-name>
    <url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

以下是我如何运作:

<?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>validation-manager</display-name>
    <!--Comment if not using H2 database-->
    <context-param>
        <param-name>db.url</param-name>
        <param-value>jdbc:h2:file:~/VM/data/validation-manager;CREATE=TRUE;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE</param-value>
    </context-param>
    <context-param>
        <param-name>db.user</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>db.password</param-name>
        <param-value/>
    </context-param>
    <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>true</param-value>
    </context-param>
    <listener>
        <listener-class>org.h2.server.web.DbStarter</listener-class>
    </listener>
    <!--End of H2 database exclusive settings-->
    <filter>
        <description>Make GB2312\GBK or others to UTF-8 for chineese character support</description>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>net.sourceforge.javydreamercsw.validation.manager.web.filter.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
            <description>Vaadin UI to display</description>
            <param-name>UI</param-name>
            <param-value>net.sourceforge.javydreamercsw.validation.manager.web.ValidationManagerUI</param-value>
        </init-param>
        <init-param>
            <description>Application widgetset</description>
            <param-name>widgetset</param-name>
            <param-value>net.sourceforge.javydreamercsw.validation.manager.web.AppWidgetSet</param-value>
        </init-param>
    </servlet>
    <servlet>
        <servlet-name>VMWebService</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>VMWebService</servlet-name>
        <url-pattern>/VMWebService</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>