Spring Boot fails to recognize path variable data sent from Angular

When working with Angular 7, I encountered a situation where I needed to pass a value from the service class of Angular. Here is how I achieved it:

executeHelloWorldBeanServiceWithPathVariable(name){
    console.log("name coming from here"+name);
    return this.httpClient.get<HelloWorldBean>('http://localhost:8080/hello-world/path-variable/${name}');
     console.log("hello world bean service executed");
   }

After testing, I confirmed that the name was successfully printed in the console as expected:

console.log("name coming from here"+name);

There were no issues with printing the name in the console.

https://i.stack.imgur.com/bHr3A.png

In my Spring Boot application, I defined the endpoint as follows:

@GetMapping(path="/hello-world/path-variable/{name}")  
    public HelloWorldBean helloWorldBeanPathVariable(@PathVariable("name") String name) {
        System.out.print("name is"+name);
        return new HelloWorldBean(String.format("Hello world %s", name));
    }

However, when trying to print the passed parameter in the Spring Boot application using

System.out.print("name is"+name);
, I faced some difficulties as the name was not being displayed in the console as expected.

This issue led me to explore further and check how the name would appear through EL expressions instead:

https://i.stack.imgur.com/QcKcE.png

Ultimately, despite the debugging challenges, the UI output showed the correct result:

https://i.stack.imgur.com/s8mGU.png

Answer №1

It appears that you may not be utilizing the correct syntax for template literals in your

executeHelloWorldBeanServiceWithPathVariable
method. I assume you are attempting to do so based on the presence of the ${} expression.

This could be causing the request not to parse correctly. Ensure that you use backticks (`) instead of single or double quotes.

executeHelloWorldBeanServiceWithPathVariable(name){
  return this.httpClient.get<HelloWorldBean>(`http://localhost:8080/hello-world/path-variable/${name}`);
}

Additionally, to effectively retrieve the observables from the API request, you will need to subscribe() to it within the component that requires it.

For example:

constructor(
  private yourService: YourService,
) { }

ngOnInit() {
  this.yourService.executeHelloWorldBeanServiceWithPathVariable('someName').subscribe(res => {
    console.log(res);
    // add further actions here
  })
}

Answer №2

Remember to always use backticks ` instead of single quotes ' or double quotes " when concatenating strings in TypeScript, especially when evaluating a value such as `${myname}`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining AngularJS with Servlets: A Seamless Integration

I am attempting to retrieve a JSON object from a servlet by calling a function through a link in my HTML code. Below is the HTML link that calls the fTest function: <td><a href="" ng-controller="minaplantaCtrl" ng-click="fTest(x.id_camion_descar ...

Capturing jQuery ajax requests in Angular

Within my Angular application, I am utilizing the integrated $.couch API from CouchDB. My goal is to intercept every ajax request, regardless of whether it originates from Angular's $http service or jQuery's $.ajax (which is utilized by $.couch). ...

How can we automatically update a component with modified values after making a post request in Angular?

getAllSchools() is a function that retrieves a list of schools from the backend when the application is loaded. This list is then stored in an array called schools. addSchool() is used to add a new school to the existing list. After adding a new school u ...

How to pass selected rows from Material-Table in React.js to another page

Is there a way to redirect to another page while passing selected row data as a prop? I am using material-table and I need to transfer the selected rows data to another page upon clicking the "Export" button, so that I can utilize the data in generating a ...

What is the most efficient method to import multiple MaterialUI components using a shared namespace without requiring the entire library to be imported

In order to prevent name mixing with other libraries, I want my MaterialUI components to be under the same namespace. For example, ensuring that <Box> references <MaterialUI.Box> and not <SomeOtherLibrary.Box>. Although it seems like a si ...

Implementing a dynamic module or dependency addition using a webpack plugin: A comprehensive guide

I'm feeling a bit frustrated because what I am trying to accomplish doesn't seem too difficult, but the documentation for webpack is so disorganized that it's eating up a lot of my time. Can someone please explain how I can inject a "dynami ...

A step-by-step guide on how to refresh a circular loading indicator

I have been researching how to create a circular progress bar using canvas, and I came across this code. After making some modifications to the code snippets that I found online, I encountered an issue - I can't seem to reload the circular path once i ...

Variable for Ajax success not set even though the other data was returned

UPDATE: While the main question remains unchanged, I have modified the approach to now return a getButtons() function instead of a global buttons array. I am utilizing an AJAX request to fetch data from the server in order to populate a popup box, which i ...

What is the best way to prevent the dropdown function in a selectpicker (Bootstrap-Select)?

Is there a way to completely disable a selectpicker when a radio button is set to "No"? The current code I have only partially disables the selectpicker: $("#mySelect").prop("disabled", true); $(".selectpicker[data-id='mySelect']").addClas ...

Angular.js and ExpressJS combination results in a 404 error for a POST request

Currently, I am attempting to make a request to my ExpressJS server using AngularJS: angular.module('app').controller('contactCtrl', function($scope, $http) { $scope.envoyer = function(nom, organisation, courriel, telephone, messag ...

Numerous Switches Similar to Tabs Using JavaScript and CSS

I'm looking to create tabs that operate as toggles, but with the twist that only one toggle can be active at any given time. Additionally, I need the tab content to appear above the actual tabs themselves. The challenge lies in the fact that I am usin ...

I'm facing some difficulties in sourcing my header into a component and encountering errors. Can anyone advise me on how to properly outsource my header?

Looking to streamline my headers in my Ionic 4 project by creating a separate reusable component for them. Here's how I set up my dashboard page with the header component: <ion-header> <app-header title="Dashboard"></app-he ...

Run retire.js commands through a Java application

I am currently utilizing the retire.js tool to detect security vulnerabilities in JavaScript files. My goal is to integrate retire.js with my Java application, allowing me to run retire.js commands directly from my Java codebase. While Maven offers a solut ...

Tips on obtaining the screen resolution and storing it in a PHP variable

Hey there! I've run into a bit of a roadblock that I'm struggling to overcome. I know that I need to incorporate some JavaScript to solve this issue, but I'm having trouble grasping how to do so. Here's the script I'm working with: ...

Exploring the realm of external variables and scoping within a jQuery function

I am facing an issue with the following code, where I want to execute test() every time the window is resized. var i = 0; var test = (function() { console.log(i++); })(); $(window).resize(function() { test(); }); Code: http://jsfiddle.net/qhoc/N ...

Verifying user input with JQuery's $.post function

I have a question regarding asynchronous calls. My goal is to validate whether a given "code" is valid without refreshing the page. Here's the scenario in brief: I need to determine if a variable should be set to true or false based on the response ...

When working with Angular 8, you may encounter an issue where the AWSIoTProvider from aws-amplify

After referencing the official link document here, I found that my code works fine with ng serve. However, after building it and trying to access the page, I encountered an error stating "AWSIoTProvider is not a constructor ". Despite searching for a sol ...

Task: Choose MySQL option and Retrieve JSON data

I have a question regarding implementing a function in a separate module file and calling it within a route to retrieve query data: function getSobre() { return new Promise((resolve, reject) => { db.query(`SELECT * FROM sobre ORDER BY cod ...

Jackson is a powerful tool in JAVA that allows you to easily parse string and convert it to JSON. Here

I'm currently attempting to transform a string into JSON using Jackson. To achieve this, I've utilized the ObjectMapper().readValue() method to deserialize it into a DragDropJsonDto object. The structure of qList.getHeader() is as follows: &quo ...

Sending the HTML input value to a Knockout view

Can someone assist me with a dilemma I'm facing? Within CRM on Demand, I have a view that needs to extract values from CRM input fields to conduct a search against CRM via web service. If duplicate records are found, the view should display them. Be ...