Vaadin,SpringBoot和Jersey获得请求返回404

时间:2016-11-14 17:06:17

标签: java spring-boot jersey vaadin

这是我在这里发表的第一篇文章,直到现在我终于找到了我的开发问题的解决方案,但现在我真的感觉好了。也许你可以帮助我。 我正在尝试基于vaadin + spring boot +设置简单的Rest get web app 在tomcat上的maven。在.pom的某些组合中,我收到了获得响应:“sth else”但在这种情况下,vaadin ui没有加载。在这种配置中,我在下面粘贴,我无法获得获得响应:

的pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.drzinks</groupId>
    <artifactId>site</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Site</name>
    <description>CRUD Vaadin Spring App</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <vaadin.version>7.7.3</vaadin.version>
        <vaadin.plugin.version>7.7.3</vaadin.plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <!-- in order to have hot deploy in tomcat -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>7.7.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>


</project>

的web.xml:

    <web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!-- Servlet declaration can be omitted in which case
         it would be automatically added by Jersey -->
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>

    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

WelcomeController.java:

    package com.drzinks;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.stereotype.Component;

@Component
@Path("/sth")
public class WelcomeController {

    @GET
    @Produces("text/plain")
    public String getSthElse(){
        return "sth elese";
    }
}

SiteApplication:

   package com.drzinks;

import javax.ws.rs.ApplicationPath;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SiteApplication {

    public static void main(String[] args) {
        SpringApplication.run(SiteApplication.class, args);
    }
}

SiteUI.java:

    package com.drzinks;

import org.springframework.beans.factory.annotation.Autowired;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SpringUI
@Theme("valo")
public class SiteUI extends UI {

    @Autowired
    private RestClient restClient;

    private VerticalLayout layout;


    @Override
    protected void init(VaadinRequest request) {
        setupLayout();
        addHeader();

    }


    private void setupLayout() {
        layout = new VerticalLayout(); //alt shift l extr local var
        setContent(layout);

    }

    private void addHeader() {
//      Label label1 = new Label(welcomeController.returnSth());
//      layout.addComponent(label1);        
        Label label2 = new Label(restClient.getSthFromServer());
//      Label label2 = new Label("restClient.getSthFromServer()");
        layout.addComponent(label2);
    }

}

RestClient.java:

   package com.drzinks;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;

import com.vaadin.spring.annotation.UIScope;

@UIScope
@Component
public class RestClient {

    private Client client = ClientBuilder.newClient();
    private WebTarget webtarget = client.target("http://localhost:8080");

    public String getSthFromServer() {
        String response;
        response = webtarget.path("sth")
                .request().get(String.class);
        return response;

    }

}

folder_structure

如果有人可以提供帮助,我会非常感激,因为我花了12个多小时才打破整晚..

0 个答案:

没有答案