Skip to content
Advertisement

How To Convert Spring Boot Entity to Angular Object

I’m trying to use Angular and Spring Boot to show a list of Rule objects. I am not using a JPA Repository to respond to the GET request, so I think I have to ‘manually’ convert my list of Rule objects on the Spring Boot side to a JSON so that httpClient.get<Rule[]>() can convert it to a list of Rule objects on the Angular side. However, this is currently not working, and my rule objects are not showing up on my webapp.

I’m not using a repository because I’m querying the database on the Spring Boot side, and doing some business logic to only display rules that fall under a certain criteria, and adding some information about the rules that is not in the database.

How do I correctly convert the list of rules to a JSON? Is this even the right approach?

Thank you!

Rule.java:

JavaScript

RuleController.java:

JavaScript

RuleListService.java:

JavaScript

rule.ts:

JavaScript

rule.service.ts:

JavaScript

rule-list.component.ts:

JavaScript

rule-list.component.html:

JavaScript

UPDATE: I’m still not seeing the data in the table, and have gotten this error from the dev console on my browser:

JavaScript

I tried changing the variables to public in the Rule.ts constructor, and accessing the variables directly instead of calling get() methods like so, {{rule._messageCode}}, for all table fields, and while it (obviously) got rid of the error, I still did not see data in the table.

The table has been populated with rows indicating there is actually a Rule[] being sent and received correctly, but there is no data in any row.

I’ve also updated all included code, since many changes have been made.

Table Screenshot

Advertisement

Answer

Posting as an answer not an update in order to help anyone who has the same issue.

  1. Moved attributes of Rule object out of constructor in Rule.ts
  2. Per @BeWu’s suggestion, got rid of the “_” prepending the attribute names in Rule.ts
  3. Changed getRules() in rule-list.component.ts to:
JavaScript

My table is now displaying all values correctly. I think the issue was stemming from the getRules() method not properly converting the data from ruleService.getRuleList() into Rule objects. I found a similar question with a great answer that had lots of useful information, linked here.

Thank you to everyone who took the time to post suggestions and guidance!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement