Ember.js与Spring Framework集成问题:保存客户字段获空值
Hey there, let's tackle your two main issues one by one — the null firstname/lastname values and that JSON parsing warning from Ember.
1. Fix the "empty string can't be parsed as valid JSON" warning
Your Spring method returns void, which makes the server send an empty response body. But Ember Data expects a valid JSON response even if it's just an empty object or null. Here's how to fix this:
- Update your
createCustomermethod to return a valid response structure:
Alternatively, if you don't need a response body, you can still return a 200 OK with a valid empty payload:@PostMapping(path="/createCustomer") public ResponseEntity<Map<String, Object>> createCustomer(@RequestBody EmberCustom createCustomer) { System.out.println("Firstname: " + createCustomer.getFirstname()); System.out.println("Lastname: " + createCustomer.getLastname()); // Your save logic here return ResponseEntity.ok(new HashMap<>()); // Returns empty JSON object {} }
This will eliminate the parsing warning from Ember.@PostMapping(path="/createCustomer") public ResponseEntity<Void> createCustomer(@RequestBody EmberCustom createCustomer) { // Your save logic here return ResponseEntity.ok().build(); }
2. Fix null firstname/lastname values
This is almost always caused by a mismatch between Ember's request structure and Spring's expected entity format. Ember Data's RESTAdapter wraps data in a data.attributes object by default, so the JSON it sends looks like this:
{ "data": { "type": "customers", "attributes": { "firstname": "John", "lastname": "Doe" } } }
Your Spring code is trying to map this directly to EmberCustom, which doesn't match the structure — hence the null values.
You have two options to fix this:
Option 1: Adjust Spring to match Ember's request structure
Create a wrapper class to handle Ember's nested format:
public class EmberRequest<T> { private EmberData<T> data; // Getters and setters public static class EmberData<T> { private String type; private T attributes; // Getters and setters } }
Then modify your controller method to use this wrapper:
@PostMapping(path="/createCustomer") public ResponseEntity<Map<String, Object>> createCustomer(@RequestBody EmberRequest<EmberCustom> request) { EmberCustom customer = request.getData().getAttributes(); System.out.println("Firstname: " + customer.getFirstname()); System.out.println("Lastname: " + customer.getLastname()); // Your save logic here return ResponseEntity.ok(new HashMap<>()); }
Option 2: Modify Ember to send flat JSON
If you don't want to change your Spring code, adjust Ember's adapter to send a flattened JSON structure:
// app/adapters/customer.js import RESTAdapter from '@ember-data/adapter/rest'; export default class CustomerAdapter extends RESTAdapter { serialize(snapshot, options) { let json = super.serialize(snapshot, options); // Extract attributes to the root level return json.data.attributes; } }
Now Ember will send JSON like this, which matches your EmberCustom entity:
{ "firstname": "John", "lastname": "Doe" }
Quick checks for your EmberCustom class
Don't forget to verify these basics:
- Field names exactly match the JSON keys (e.g.,
firstnamein both JSON and Java, case-sensitive) - The class has a no-argument constructor (Spring needs this to instantiate the object)
- All fields have corresponding getter and setter methods
For easier testing, use Postman to send a sample JSON request to /createCustomer first — this will confirm if Spring can correctly parse the fields before you debug the Ember side.
内容的提问来源于stack exchange,提问作者EllieJ

