FAQ: Responding to Requests with Spring - Deserializing into Objects

This community-built FAQ covers the “Deserializing into Objects” exercise from the lesson “Responding to Requests with Spring”.

Paths and Courses
This exercise can be found in the following Codecademy content:

(Beta) Create REST APIs with Spring and Java

FAQs on the exercise Deserializing into Objects

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

curl isn’t working it says cannot connect to localhost 4001 because it refuses.

4 Likes

I know it’s been several months since your post. I ran into this too and here is what I did to successfully POST a new super hero. Hopefully this helps others going through this course. :slight_smile:

In completing Step 1 (import RequestBody, change method’s parameters, update method body), I clicked the Run button and noticed the following output from the Maven build.

[23,23] constructor SuperHero in class com.codecademy.ccapplication.SuperHero cannot be applied to given types;
[ERROR]   required: no arguments
[ERROR]   found: java.lang.String,java.lang.String,java.lang.String
[ERROR]   reason: actual and formal argument lists differ in length

So I checked out the SuperHero.java file to see its constructor. This can be done by clicking the folder icon to the left of the open file tabs.

Upon review of the SuperHero.java file, I noticed the class body was empty and indeed there was no constructor or fields.

@Entity
public class SuperHero {

  
}

I checked out the Get Unstuck option and viewed the solution. Opening the SuperHero.java solution file showed the file in my environment was missing instance fields, a constructor, and getters. I copied and pasted this code into the SuperHero.java file in my environment.

@Entity
public class SuperHero {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  private String firstName;
  private String lastName;
  private String superPower;

  protected SuperHero() {}

  public SuperHero(String firstName, String lastName, String superPower) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.superPower = superPower;
  }

  @Override
  public String toString() {
    return String.format(
        "Super Hero[id=%d, firstName='%s', lastName='%s', superPower='%s']",
        id, firstName, lastName, superPower);
  }

  public Long getId() {
    return id;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String getSuperPower(){
    return superPower;
  }
}

After clicking the Run button again, Maven successfully completed the build process with the following output:

[INFO] BUILD SUCCESS

Then I was able to make the POST request.

curl -X POST -H "Content-Type: application/json" --data "{\"firstName\": \"Thor\", \"lastName\": \"Odinson\", \"superPower\": \"A good hammering with Mjolnir.\"}" localhost:4001/superHeroes/addNew

And check that it was successful.

curl localhost:4001/superHeroes

...
, {
  "id" : 11,
  "firstName" : "Thor",
  "lastName" : "Odinson",
  "superPower" : "A good hammering with Mjolnir."
} ]

Hopefully this helps.

2 Likes