【问题标题】:Spring boot application working in IntelliJ, but not as Docker ContainerSpring Boot 应用程序在 IntelliJ 中工作,但不能作为 Docker 容器
【发布时间】:2018-05-23 13:51:36
【问题描述】:

我创建了一个 Spring 启动应用程序,它通过 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;
    }
}

Tweet(删除了 getter/setter):

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 容器后,我再次向 docker 容器内运行的 Spring 引导服务发送完全相同的 Post 请求,但它失败并出现异常

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 是否更改了请求的正文或为什么它不起作用?

【问题讨论】:

  • Docker 不会更改请求的正文。你如何启动你的 docker 镜像?分析的关键字列表是什么样的?需要更多信息...
  • @Ivonet 我添加了其他信息。
  • 你可能想看看:stackoverflow.com/questions/20837856/…,因为它不一定是 docker 问题。其余的我并没有立即发现有什么问题。
  • 你能评论所有代码并在处理程序中打印一条消息,看看它是否有效吗?之后也尝试打印收到的 JSON。尝试逐个添加代码段以查明问题所在。我怀疑它是 docker,但是 docker images JDK 和你的机器 JDK 可能不同。可能还有其他可能不同的东西。所以你需要弄清楚它是什么
  • @TarunLalwani 我已将参数从 AnalyzedKeyworldList 更改为 String,并且只有一个 System.out.println,但它仍然因相同的错误而崩溃。它甚至不会进入方法。

标签: json spring docker spring-boot docker-compose


【解决方案1】:

如果您的问题是由旧代码引起的,那么您可以做两件事来确保构建了最新的代码

docker-compose build
docker-compose up

docker-compose up --build

【讨论】:

  • 感谢您的回答。我尝试了该命令,但它没有工作。我认为我的配置有问题。
【解决方案2】:

我找到了解决问题的方法。我的代码运行良好,但运行命令时似乎 Docker VM 没有更新

docker-compose up

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

我不知道为什么会这样,因为Docker docs states:

如果存在服务的现有容器,并且该服务的 容器创建后配置或图像被更改, docker-compose up 通过停止并重新创建 容器(保留已安装的卷)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 2021-08-08
    • 2018-06-25
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2018-07-11
    • 2014-09-26
    相关资源
    最近更新 更多