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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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.

3 Likes

but why does the id starts from 10 all others are 1 2 3 and suddenly 10 11 12 13 14 idont get it

The reason the id starts at 10 when a new superHero is added is because the id generator strategy of “GenerationType.AUTO” doesn’t differentiate between the two classes of “SuperHero” and “SuperReport”. Therefore, the count starts from 1 and ends at 9 when the code is run to create the below 9 objects regardless of class. See the below articles that explain in more detail the different strategies and why “GenerationType.AUTO” exhibits this behavior. To resolve this issue, it’s possible to use “strategy=GenerationType.IDENTITY” in both classes instead of “strategy=GenerationType.AUTO”.

  // save a few customers
  repository.save(new SuperHero("Fran", "Fling", "Flying")); 
  repository.save(new SuperHero("Wendy", "Welsh", "Water Bending"));
  repository.save(new SuperHero("Mary", "Macky", "Mind Reading"));
  repository.save(new SuperHero("Ingrid", "Immerson", "Invisibility"));

  repository2.save(new SuperReport("64672", "123 Main St", "2020-05-04"));
  repository2.save(new SuperReport("31008", "674 Broad St", "2018-08-12"));
  repository2.save(new SuperReport("64672", "987 East Dr", "2020-09-08"));
  repository2.save(new SuperReport("64672", "546 South Blvd", "2020-12-29"));    
  repository2.save(new SuperReport("31008", "712 Early St", "2021-01-03"));

It doesn’t work. I even tried pasting the code put in the “stuck” section

curl -d "{ \"firstName\": \"Diana\", \"lastName\": \"Prince\", \"superPower\": \"super strength\" }" -H "Content-Type: application/json" localhost:4001/superHeroes/addNew

The reason

curl: (7) Failed to connect to localhost port 4001: Connection refused

try pkill ein in bash and do the curl again

1 Like

Yes this continues to be a Bug.

There is no instruction on adding any of those annotations, nor any description of what they are. I’m not sure how this has lasted this long without being addressed.

You can report bugs within the lessons, rather than reporting them here where no one from CC will see them.

Get Unstuck>Bugs

That way, there’s a record of the issue internally.