Spring Boot应用程序可以在IntelliJ中运行,但不能作为Docker容器运行

收费的

我创建了一个Spring Boot应用程序,该应用程序通过HTTP Post将一些经过分析的Twitter内容作为JSON对象获取。JSON对象如下所示:

{
    "analyzedKeywords": [
        {
            "keyword": "VW",
            "tweets": [
                {
                    "indicoScore": 0.8174982823,
                    "popularity": 5659,
                    "tweet": {
                        "createdAt": 1512660826000,
                        "favouriteCount": 0,
                        "retweet": true,
                        "retweetCount": 5,
                        "retweetedStatus": {
                            "createdAt": 1512660253000,
                            "favouriteCount": 1,
                            "retweet": false,
                            "retweetCount": 5,
                            "retweetedStatus": null,
                            "tweetText": "No time for twitter drama because those VW Polo's aren't gonna strip themselves",
                            "user": {
                                "email": null,
                                "favouritesCount": 1154,
                                "followersCount": 1080,
                                "friendsCount": 295,
                                "id": 197398224,
                                "profileImageURL": "http://pbs.twimg.com/profile_images/872393691427745792/8DhxJY5-_normal.jpg",
                                "statusesCount": 120014,
                                "username": "Kabelo"
                            }
                        },
                        "tweetText": "No time for twitter drama because those VW Polo's aren't gonna strip themselves ",
                        "user": {
                            "email": null,
                            "favouritesCount": 9820,
                            "followersCount": 5654,
                            "friendsCount": 558,
                            "id": 58419134,
                            "profileImageURL": "http://pbs.twimg.com/profile_images/936993708142157825/BgvNafEp_normal.jpg",
                            "statusesCount": 124848,
                            "username": "\ud83c\udf93 Mmina T\u0161hipi \ud83c\udf93"
                        }
                    }
                }
            ]           
        },
        {
            "keyword": "Tesla",
            "tweets": [
                {
                    "indicoScore": 0.9143414881,
                    "popularity": 10027,
                    "tweet": {
                        "createdAt": 1512660797000,
                        "favouriteCount": 0,
                        "retweet": true,
                        "retweetCount": 4,
                        "retweetedStatus": {
                            "createdAt": 1512602297000,
                            "favouriteCount": 5,
                            "retweet": false,
                            "retweetCount": 4,
                            "retweetedStatus": null,
                            "tweetText": "Anyone know of a plug-in vehicle that can seat 6 and, preferably, tow? \nSo far, our list includes the @Tesla Model\u2026 ",
                            "user": {
                                "email": null,
                                "favouritesCount": 28,
                                "followersCount": 39,
                                "friendsCount": 13,
                                "id": 930140890189975553,
                                "profileImageURL": "http://pbs.twimg.com/profile_images/931266152973484032/I6PltHR1_normal.jpg",
                                "statusesCount": 32,
                                "username": "InsideEVs Forum"
                            }
                        },
                        "tweetText": "Anyone know of a plug-in vehicle that can seat 6 and, preferably, tow? \nSo far, our list includes the @Tesla Model\u2026 ",
                        "user": {
                            "email": null,
                            "favouritesCount": 6,
                            "followersCount": 10023,
                            "friendsCount": 18,
                            "id": 568621669,
                            "profileImageURL": "http://pbs.twimg.com/profile_images/894917277925158914/nZefv1rw_normal.jpg",
                            "statusesCount": 20263,
                            "username": "InsideEVs"
                        }
                    }
                }
                ]
        }
            ]
        }

获取JSON的方法如下所示:

@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<byte[]> Post(@RequestBody AnalyzedKeywordList analyzedKeywords) {
    Document document = new Document();

    PdfWriter writer = null;
...

当我从IntelliJ运行代码并将此JSON发布到我的服务时,AnalyzedKeyWordList充满了关键字对象“ VW”和“ TESLA”。这样就行了。

类“ AnalyzedKeywordList”看起来像这样:

导入java.util.List;

public class AnalyzedKeywordList {
    List<AnalyzedKeyword> analyzedKeywords;

    public AnalyzedKeywordList(List<AnalyzedKeyword> analyzedKeywords) {
        this.analyzedKeywords = analyzedKeywords;
    }

    public AnalyzedKeywordList(){}

    public List<AnalyzedKeyword> getAnalyzedKeywords() {
        return analyzedKeywords;
    }

    public void setAnalyzedKeywords(List<AnalyzedKeyword> analyzedKeywords) {
        this.analyzedKeywords = analyzedKeywords;
    }
}

AnalyzedKeyword看起来像这样(我删除了getter和setter使其更短):

public class AnalyzedKeyword {
    private String keyword;
    private List<AnalyzedTweet> tweets;

    public AnalyzedKeyword(){}
}

AnalyzedTweet(我删除了getter和setter方法,使其更短):

public class AnalyzedTweet {

    private float indicoScore;
    private Tweet tweet;
    private float popularity;

    public AnalyzedTweet(){}

    public AnalyzedTweet(float indicoScore, Tweet tweet, float popularity) {
        this.indicoScore = indicoScore;
        this.tweet = tweet;
        this.popularity = popularity;
    }
}

推文(删除吸气剂/设置器):

public class Tweet {

    private String tweetText;
    private boolean isRetweet;
    private Date createdAt;
    private float favouriteCount;
    private float retweetCount;
    private Tweet retweetedStatus;
    private TwitterUser user;

    public Tweet(){}
}

TwitterUser(已删除getter / setter):

public class TwitterUser {
    private long id;
    private String username;
    private String email;
    private String profileImageURL;
    private float followersCount;
    private float friendsCount;
    private float favouritesCount;
    private float statusesCount;

    public TwitterUser(){}
}

现在,我正在编译一个.jar文件,并使用docker对其进行组合(以及其他一些服务):

FROM openjdk:8
ADD target/report-service.jar report-service.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","report-service.jar"]

启动Docker容器后,我再次将完全相同的Post请求发送给运行在Docker容器中的Spring引导服务,但由于失败而失败

WARN 1 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
report-service_1   |  at [Source: java.io.PushbackInputStream@4086f71a; line: 3, column: 1]

我正在通过``docker-compose up''启动我的Docker容器。这还会创建一些运行良好的其他容器。

version: '3'
services:
    twitter-service:
        build: ./twitter
        ports:
            - "5000:8080"

    analyse-service:
        build: ./analysis_py
        volumes:
            - ./analysis_py:/usr/src/app
        ports:
            - "5001:80"

    report-service:
        build: ./report
        ports:
            - "5002:8080"

    frontend:
        build: ./frontend # specify the directory of the Dockerfile
        #volumes:
        #    - ./frontend:/usr/src/app
        ports:
            - "4200:4200"

泊坞窗是否更改了请求的主体,或者为什么它不起作用?

收费的

我为我的问题找到了解决方案。我的代码工作正常,但运行命令时似乎未更新Docker VM

docker-compose up

并且部署了我的Spring Boot应用程序的旧版本,而不是新版本。当我关闭Docker,删除虚拟机并运行上面的命令时,Docker创建了一个新的VM,它可以完美地工作。

我不知道为什么会这样,因为Docker文档指出

如果已有用于服务的容器,并且在创建容器后更改了服务的配置或映像,则docker-compose up会通过停止并重新创建容器(保留装入的卷)来获取更改。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot 应用程序通过 Maven 运行良好,但不能通过 IDE Intellij IDEA

如何访问在Docker容器中运行的Spring应用程序?

在Docker容器中运行Spring Boot应用程序,无法连接MySQL

spring boot 应用程序未连接到在 docker 容器中运行的 activemq

如何在intellij中运行spring boot应用程序?

Intellij Spring Boot应用程序无法在Tomcat中运行

无法从容器外部运行的Spring Boot应用程序连接到容器中运行的Kafka

无法加载在Docker中运行的Spring Boot应用程序

用于在 docker 容器上运行 spring boot 应用程序的 Docker 文件?

在我的 Spring Boot 应用程序(ELK)的 Docker 容器中运行的 Kibana 中看不到日志信息

运行 Spring Boot 应用程序的 Docker 容器不响应请求

运行 spring 应用程序的 Jetty Docker 容器无法连接到在 docker 容器外运行的 mysql

为什么Spring Bean可以在Tomcat Servlet容器中运行但不能在EJB中运行?

在不同的Docker容器中运行的Spring Boot应用拒绝连接

为什么我的Spring Boot Web应用程序不能在Gradle中完全运行?

使用Spring Boot和多模块进行Maven配置-在Intellij中运行应用程序

在IntelliJ中运行Spring Boot Gradle应用程序时始终获取404

Docker-compose-为运行Spring Boot独立应用程序的Docker容器提供基于XML的配置

在 intellij idea 中运行 spring mvc 应用程序的问题

Spring Boot应用程序运行失败

在容器中运行 Spring Boot 应用程序?还是虚拟机?还是虚拟机内的容器?

我可以在Spring Boot应用程序中运行长任务吗?

在Spring Boot中应用程序运行失败

尝试在 docker 中运行 spring boot 应用程序时出现 java.lang.NoClassDefFoundError

在Docker中运行Spring Boot应用程序时发生异常

是否可以将非容器化的 Spring Boot 应用程序连接到 MongoDB Docker 容器?

通过在同一容器中运行多个应用程序来使Spring Boot中的配置外部化

Spring Boot:作为Java应用程序运行,但类路径包含spring-web

如何制作其他人可以使用 Docker 运行的 Spring Boot 应用程序