Information sent from TypeScript frontend to Java backend is converted into a LinkedHashMap

I have a situation where my typescript frontend is communicating with my java backend using REST. Recently, I added a new simple rest endpoint but encountered an issue when trying to cast the sent object properly because the body being sent is a LinkedHashMap.

service.ts

createEvent(event: CustomerEvent): Observable<HttpResponse<CustomerEvent>> {
    return this.http.post<CustomerEvent>(this.eventUrl, event, { observe: 'response' });
}

customer-event-model.ts

export class CustomerEvent {
    constructor(public customer: ICustomer, public event: String) {}
}

customer.model.ts

export interface ICustomer {
    id?: number;
    name?: string;
    email?: string;
    address?: string;
}

export class Customer implements ICustomer {
    constructor(public id?: number, public name?: string, public email?: string, public address?: string) {}
}

Rest

public ResponseEntity<Object> addCustomerEvent(@RequestBody Object customerEvent) {
  log.info("~REST request to create new event " + customerEvent.toString());
  CustomerEvent e = (CustomerEvent) customerEvent; // <<---- FAILS cannot cast LinkedHashMap to CustomerEvent
}

Does anyone have insight into why it's sending a LinkedHashMap instead of the expected type?

Appreciate any help. Thank you!

Answer №1

In order to properly handle the request, make sure to utilize:

@RequestBody CustomerEvent customerEvent

This approach enables Spring to accurately convert the request body into a CustomerEvent object.

For more information, check out:

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

Java thread exception causing error

Hi everyone, I'm currently working on a program that reverses user input. Here's my code so far: import java.util.*; public class Reverse { public static void main(String[] args) { Scanner in = new Scanner(System.in); List& ...

establish the data type for the key values when using Object.entries in TypeScript

Task Description: I have a set of different areas that need to undergo processing based on their type using the function areaProcessor. Specifically, only areas classified as 'toCreate' or 'toRemove' should be processed. type AreaType ...

Discovering a way to retrieve objects from an array of objects with matching IDs

Here is a code snippet I put together to illustrate my objective. arr = [ { id:1 , name:'a', title: 'qmummbw' }, { id:2 , name:'b', title: 'sdmus' }, { id:2 , name:'', title: 'dvfv' }, ...

Error: Angular version 15 is unable to locate the module '@env/environment' or its corresponding type declarations

Recently, I developed an Angular 15 application with the environments folder located under src. Here is a snippet from my tsconfig.json file: "baseUrl": "./src", "paths": { "@app/*": [ "app/*" ], "r ...

Creating a digital collection using Vue, Typescript, and Webpack

A short while back, I made the decision to transform my Vue project into a library in order to make it easier to reuse the components across different projects. Following some guidelines, I successfully converted the project into a library. However, when ...

Discovering code examples embedded within eclipse

Not too long ago, a friend of mine shared with me a useful trick for finding built-in code snippets in eclipse. These snippets offered helpful examples on how to structure things like for loops, while loops, and arrays. Unfortunately, I've since forg ...

TypeScript compiler encountering issue with locating immutable.js Map iterator within for of loop

I am currently facing a challenge with using immutable.js alongside TypeScript. The issue lies in convincing the TypeScript compiler that a Map has an iterator, even though the code runs smoothly in ES6. I am perplexed as to why it does not function correc ...

Is React Typescript compatible with Internet Explorer 11?

As I have multiple React applications functioning in Internet Explorer 11 (with polyfills), my intention is to incorporate TypeScript into my upcoming projects. Following the same technologies and concepts from my previous apps, I built my first one using ...

The use of React hooks in an application saga led to an issue where, despite using history.push, the component

Thank you for checking out this question. I have created a login demo using react hooks and TypeScript. I have some concerns: 1. When the login is successful, I use history.push('/home') to navigate to the home page, but the page also renders a N ...

Navigational menu routing with AngularJS2 using router link paths

Currently, I am working on developing a navigation menu using angularJS2. Here is the snippet from my app.component.ts: import {provide, Component} from 'angular2/core'; import {APP_BASE_HREF, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, HashLocati ...

Having trouble iterating over an array in Angular's TypeScript

I've been attempting to loop through an array of objects in TypeScript, but encountered the following error: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'forEach' of undefined TypeError: Cannot read property 'fo ...

Tips for converting a JSON array into a Java Object with GSON

I am currently working with a JSON document that needs to be parsed. Although I am displaying only two objects in the files array, typically there are more than 500 objects present. { "files":[ { "name":"/testing01/partition_395.sh ...

Tips for choosing various alternative checkboxes in Selenium WebDriver

driver.get("183.82.103.245/nareshit/login.php"); driver.findElement(By.name("txtUserName")).sendKeys("nareshit"); driver.findElement(By.name("txtPassword")).sendKeys("nareshit"); driver.findElement(By.nam ...

Guide on utilizing the useContext hook in React/Next.js while incorporating TypeScript

As I embark on my journey in the world of TypeScript, I find myself working on a new project in Next.js using TypeScript. My goal is to incorporate authentication functionality into this project by utilizing createContext. Coming from a background in JavaS ...

Troubleshooting TypeScript Modules in Visual Studio 2015 Update 2: Fixing the 'require' Undefined Error

While working in Visual Studio 2015 Enterprise with Update 2 installed, I decided to create a new TypeScript project called TypeScriptHTMLApp1 using the default template and settings. As part of this project, I added a new TypeScript file named log.ts and ...

What steps can I take to resolve the error message stating "Type X is not compatible with Type Y" in TypeScript?

I'm encountering an issue with the middleware in my express app. Here is the code: app.use(function(req, res, next) { let _end = res.end; res.end = function end(chunk, encoding) { ... return _end.call(res, chunk, encoding); }; next(); ...

ReactNative: When attempting to navigate, a TypeError occurred - this.props.navigation.navigate is not a function

It's confusing to see how this issue is occurring, even though all props have been properly typed. The goal is to pass the navigator to the bottom bar component in order to navigate onPress. Initially, I define the props interface: export interface B ...

NodeJS function does not pause for the PostgreSQL database call despite using await keyword

I am attempting to recursively insert entries into the database where each entry depends on the previous one (the ID of the previous entry will be the child_id of the next entry). However, I am facing difficulties in getting async/await to work correctly. ...

Updating an object property within an array in Angular Typescript does not reflect changes in the view

I am currently delving into Typescript and Angular, and I have encountered an issue where my view does not update when I try to modify a value in an array that is assigned to an object I defined. I have a feeling that it might be related to the context b ...

Visibility in classes can shift once a new file is imported

Currently, I am encountering a puzzling issue in my angular2 project using typescript. In my main.ts file, which contains a component along with some imports at the start of the file, there is a custom type class (let's call it TypeFoo) located in mod ...