简体   繁体   中英

Jackson doesn't convert Object into Json String

I'm trying to convert my Guestbook Object into a Json String, but the Objectmapper won't accept my Object. mapper.writeValueAsString(GuestbookEntry) throws an Error.


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GuestbookEntry extends Entry{
    private String author;
    private String content;

 public GuestbookEntry(String author, String content) throws IllegalArgumentException  {
        super();
       if(author == null || content == null){
            throw new IllegalArgumentException ("wrong entry");
        }
        Matcher hasMatch = special.matcher(author);
        Matcher hasMatch2 = special2.matcher(content);

        // Überprüfe Sonderzeichen
        if (hasMatch.find() || hasMatch2.find()){
            throw new IllegalArgumentException("wrong entry");
        }
        this.author = author;
        this.content = content;
    }




    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        if(author == null ){
            throw new NullPointerException("no name");
        }
        Matcher hasMatch = special.matcher(author);

        if (hasMatch.find()){
            throw new IllegalArgumentException("wrong entry");
        }
            this.author = author;

    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        if(content == null){
            throw new NullPointerException("no entry");

        }
        Matcher hasMatch2 = special2.matcher(content);


        if (hasMatch2.find()){
            throw new IllegalArgumentException("wrong entry");
        }
        this.content = content;


    }




    @Override
    public String toJson() {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode rootNode = mapper.createObjectNode();
        try {
            String JsonString = mapper.writeValueAsString(GuestbookEntry);
          return JsonString;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

mapper.writeValueAsString seems to require an Expression, but I've seen people convert Objects into Json Strings before. I ve already tried different inputs, but I ve no idea what to do.

It tells me: "cannot resolve symbol Guestbookentry".

My Input is:

GuestbookEntry test = new GuestbookEntry("testname", "test entry");
System.out.println(test.toJson());

And I want to get something like:

{id:12345624553, date:"2021-07-02T21:12:50.437600", author:"testname", content:"test entry"};

You have a compiler error, because you are passing class reference name GuestbookEntry to a mapper instead of an instance in toJSON method. To make it work, pass this keyword:

mapper.writeValueAsString(this);

It will hold class instance which mapper will serialize.

Also, if you want to deserialize it later, you'll have to provide a default constructor for this class, or put Jackson annotations JsonProperty on an existing constructor.

To read more about jackson annotations: https://www.baeldung.com/jackson-annotations

Try this as mentioned it has a compiler error

package test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GuestbookEntry extends Entry {
private String author;
private String content;

public GuestbookEntry(String author, String content) throws IllegalArgumentException {
    super();
    if (author == null || content == null) {
        throw new IllegalArgumentException("wrong entry");
    }
    Matcher hasMatch = special.matcher(author);
    Matcher hasMatch2 = special2.matcher(content);

    // Überprüfe Sonderzeichen
    if (hasMatch.find() || hasMatch2.find()) {
        throw new IllegalArgumentException("wrong entry");
    }
    this.author = author;
    this.content = content;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    if (author == null) {
        throw new NullPointerException("no name");
    }
    Matcher hasMatch = special.matcher(author);

    if (hasMatch.find()) {
        throw new IllegalArgumentException("wrong entry");
    }
    this.author = author;

}

public String getContent() {
    return content;
}

public void setContent(String content) {
    if (content == null) {
        throw new NullPointerException("no entry");

    }
    Matcher hasMatch2 = special2.matcher(content);

    if (hasMatch2.find()) {
        throw new IllegalArgumentException("wrong entry");
    }
    this.content = content;

}

@Override
public String toJson() {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode rootNode = mapper.createObjectNode();
    try {
        String JsonString = mapper.writeValueAsString(this);
        return JsonString;
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return null;
}

public static void main(String a[]) {
    GuestbookEntry test = new GuestbookEntry("testname", "test entry");
    System.out.println(test.toJson());
}

}

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