OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to send pure JSON object (which resided inside model object) to view in spring boot?

  • Thread starter Thread starter santhosh136
  • Start date Start date
S

santhosh136

Guest
I'm new to spring boot development. I have to put my json object inside my model object and send it to view. I've used jackson library's ObjectMapper class to convert the object into String.

My controller snippet is

Code:
@GetMapping("/show-employee")
public String showEmployee(Model model) {
    ObjectMapper objectMapper = new ObjectMapper();
    String empString = objectMapper.writeValueAsString(new Employee("santhosh", "kumar", "[email protected]"))
    model.addAttribute("employee", empString);
    return "employees/employee-display";
}

And my model class is

Code:
@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @NotBlank
    @Size(min = 3, max = 20)
    private String firstName;

    @NotBlank
    @Size(min = 3, max = 20)
    private String lastName;
    
    @Email(message = "Please enter a valid email id")
    private String email;

    // constructors, getters and setters

On the view side, I have thymeleaf code as below to access the JSON object

Code:
<script>
        var employeesJsonStr = "[[${employee}]]";
        console.log(employeesJsonStr);
</script>

But on the console window, I end up with this...

Code:
{&quot;id&quot;:0,&quot;firstName&quot;:&quot;santhosh&quot;,&quot;lastName&quot;:&quot;kumar&quot;,&quot;email&quot;:&quot;[email protected]&quot;,&quot;projects&quot;:null}

How can pass the JSON String to front end so that I can access that using Javascript without having to do html decoding.

<p>I'm new to spring boot development. I have to put my json object inside my model object and send it to view. I've used jackson library's ObjectMapper class to convert the object into String.</p>
<p>My controller snippet is</p>
<pre><code>@GetMapping("/show-employee")
public String showEmployee(Model model) {
ObjectMapper objectMapper = new ObjectMapper();
String empString = objectMapper.writeValueAsString(new Employee("santhosh", "kumar", "[email protected]"))
model.addAttribute("employee", empString);
return "employees/employee-display";
}
</code></pre>
<p>And my model class is</p>
<pre><code>@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotBlank
@Size(min = 3, max = 20)
private String firstName;

@NotBlank
@Size(min = 3, max = 20)
private String lastName;

@Email(message = "Please enter a valid email id")
private String email;

// constructors, getters and setters
</code></pre>
<p>On the view side, I have thymeleaf code as below to access the JSON object</p>
<pre><code><script>
var employeesJsonStr = "[[${employee}]]";
console.log(employeesJsonStr);
</script>
</code></pre>
<p>But on the console window, I end up with this...</p>
<pre><code>{&quot;id&quot;:0,&quot;firstName&quot;:&quot;santhosh&quot;,&quot;lastName&quot;:&quot;kumar&quot;,&quot;email&quot;:&quot;[email protected]&quot;,&quot;projects&quot;:null}
</code></pre>
<p>How can pass the JSON String to front end so that I can access that using Javascript without having to do html decoding.</p>
 

Latest posts

I
Replies
0
Views
1
Isaac P. Liu
I
U
Replies
0
Views
1
user3658366
U
G
Replies
0
Views
1
Giampaolo Levorato
G
M
Replies
0
Views
1
Marcelo Rodrigo Nascimento
M
Top