简体   繁体   中英

Spring boot application working in IntelliJ, but not as Docker Container

I've created an Spring boot application which gets some analyzed Twitter-stuff as JSON object via HTTP Post. The JSON Object looks like this:

{
    "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"
                        }
                    }
                }
                ]
        }
            ]
        }

The method which gets the JSON looks like this:

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

    PdfWriter writer = null;
...

When I am running my code from IntelliJ and posting this JSON to my service, the AnalyzedKeyWordList is filled with the keywords objects "VW" and "TESLA". So it works.

The class 'AnalyzedKeywordList' looks like this:

import 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 looks like this (I've deleted the getters and setters to make it shorter):

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

    public AnalyzedKeyword(){}
}

AnalyzedTweet(I've deleted the getters and setters to make it shorter):

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(removed getters/setters):

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 (removed getters/setters):

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(){}
}

Now I am compiling a .jar file and use docker to compose it (with some others services):

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

After starting the Docker container, I am sending the exact same Post request again to the Spring boot service running inside the docker container, but it fails with the exception

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]

I'm starting my Docker container via 'docker-compose up'. this also creates some other containers which are working fine.

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"

Does docker change the body of the request or why isn't it working?

If your issue was because of old code then you could have done two things to make sure you get latest code was built

docker-compose build
docker-compose up

or

docker-compose up --build

I found a solution for my problem. My code is working fine, but it seems that the Docker VM isn't updated when I run the command

docker-compose up

and the old version of my Spring Boot application was deployed instead of the new one. When I closed Docker, deleted the Virtual Machine and ran the command above, Docker created a new VM and it worked flawlessly.

I don't know why this happens, because the Docker docs states :

If there are existing containers for a service, and the service's configuration or image was changed after the container's creation, docker-compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM