为什么我不需要 JSON.stringify() 这个 post 负载?

Why I don't need to JSON.stringify() this post payload?

我已阅读:

并且都建议在执行 POST 请求之前创建 headers 并将我的 object stringfy 到 JSON,这有效,但我注意到如果我只是通过object 它也可以正常工作。

所以我只是想知道谁对这种行为负责,因为我相信某个层正在将 object 转换为 JSON 格式。还是不行?

我正在使用:

我有一个person.component.ts,方法如下:

addPerson() {
    this.personService.save(this.person)
       .then(result => {
        console.log('Added!', result);
       });
  }

a person.service.ts 使用 save 方法:

save(person: Person){
    //OPTION 3
    return this.http.post(`${this.baseURL}person/add`, person).toPromise();
}

后端我有我的 REST 应用程序 class:

@ApplicationPath("/rest")
public class HouseApplication extends Application {

    public HouseApplication() {}
}

和我的个人资源:

@RequestScoped
@Path("/person")
@Produces({ "application/json" })
@Consumes({ "application/json" })
public class PersonResource {

    public PersonResource() {}

    @Inject
    PersonService personService;

    @POST
    @Path("/add")
    public void addPerson(Person person) throws HouseValidationException{
        personService.add(person);
    }
    ...

我实际上在 front-end 中遇到异常,但我的记录 持久化 到数据库。我认为异常与我正在使用的按钮或屏幕中的 PRIMEng UI 组件有关...

通话后我的控制台中出现以下消息:

ERROR Error: Uncaught (in promise): Unexpected literal at position 2
    at resolvePromise (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:710:31) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:761:17) [angular]
    at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4322:37) [angular]
    at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
    at HTMLButtonElement.ZoneTask.invoke (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:464:25) [<root>]
    at ____________________Elapsed_3_ms__At__Fri_May_05_2017_02_45_38_GMT_0300__BRT_ (http://localhost)
    at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
    at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
    at scheduleResolveOrReject (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:756:14) [angular]
    at ZoneAwarePromise.then (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:845:17) [angular]
    at NgModel._updateValue (eval at <anonymous> (http://localhost:8080/house/vendor.js:72:1), <anonymous>:4434:27) [angular]
    at NgModel.ngOnChanges (eval at <anonymous> (http://localhost:8080/house/vendor.js:72:1), <anonymous>:4316:18) [angular]
    at checkAndUpdateDirectiveInline (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:10907:19) [angular]
    at checkAndUpdateNodeInline (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12289:17) [angular]
    at checkAndUpdateNode (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12257:16) [angular]
    at debugCheckAndUpdateNode (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12886:59) [angular]
    at debugCheckDirectivesFn (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12827:13) [angular]
    at Object.eval [as updateDirectives] (ng:///AppModule/PatientComponent.ngfactory.js:2577:5) [angular]
    at Object.debugUpdateDirectives [as updateDirectives] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12812:21) [angular]
    at checkAndUpdateView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12224:14) [angular]
    at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12539:17) [angular]
    at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
    at checkAndUpdateView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12230:5) [angular]
    at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12539:17) [angular]
    at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
    at checkAndUpdateView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12230:5) [angular]
    at callWithDebugContext (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:13212:42) [angular]
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12752:12) [angular]
    at ViewRef_.detectChanges (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:10321:63) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5258:63) [angular]
    at Array.forEach (native) [angular]
    at ApplicationRef_.tick (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5258:25) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5138:106) [angular]
    at Object.onInvoke (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4331:37) [angular]
    at NgZone.run (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4200:62) [angular]
    at Object.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5138:82) [angular]
    at SafeSubscriber.schedulerFn [as _next] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4034:52) [angular]
    at SafeSubscriber.__tryOrUnsub (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:236:16) [angular]
    at SafeSubscriber.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:185:22) [angular]
    at Subscriber._next (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:125:26) [angular]
    at Subscriber.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:89:18) [angular]
    at EventEmitter.Subject.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:79:1), <anonymous>:55:25) [angular]
    at EventEmitter.emit (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4020:76) [angular]
    at NgZone.checkStable (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4296:40) [angular]
    at NgZone.onLeave (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4372:14) [angular]
    at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4325:27) [angular]
    at HTMLButtonElement.ZoneTask.invoke (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:460:38) [<root>]
    at ____________________Elapsed_18597_ms__At__Fri_May_05_2017_02_45_19_GMT_0300__BRT_ (http://localhost)
    at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
    at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
    at zoneAwareAddListener (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:1453:14) [angular]
    at HTMLButtonElement.addEventListener (eval at createNamedFn (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1)), <anonymous>:3:43) [angular]
    at DomEventsPlugin.addEventListener (eval at <anonymous> (http://localhost:8080/house/vendor.js:186:1), <anonymous>:3131:17) [angular]
    at EventManager.addEventListener (eval at <anonymous> (http://localhost:8080/house/vendor.js:186:1), <anonymous>:2477:23) [angular]
    at EmulatedEncapsulationDomRenderer2.DefaultDomRenderer2.listen (eval at <anonymous> (http://localhost:8080/house/vendor.js:186:1), <anonymous>:2990:36) [angular]
    at DebugRenderer2.listen (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:13476:30) [angular]
    at listenToElementOutputs (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:9558:66) [angular]
    at createViewNodes (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12148:17) [angular]
    at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12546:13) [angular]
    at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
    at createViewNodes (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12203:5) [angular]
    at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12546:13) [angular]
    at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
    at createViewNodes (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12203:5) [angular]
    at createRootView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12081:5) [angular]
    at callWithDebugContext (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:13212:42) [angular]
    at Object.debugCreateRootView [as createRootView] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12673:12) [angular]
    at ComponentFactory_.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:10017:46) [angular]
    at ComponentFactoryBoundToModule.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3633:29) [angular]
    at ApplicationRef_.bootstrap (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5215:57) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5002:79) [angular]
    at Array.forEach (native) [angular]
    at PlatformRef_._moduleDoBootstrap (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5002:42) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4964:27) [angular]
    at Object.onInvoke (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4331:37) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:758:57) [angular]
    at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4322:37) [angular]
    at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
    at <anonymous> [<root>]
    at ____________________Elapsed_62_ms__At__Fri_May_05_2017_02_45_19_GMT_0300__BRT_ (http://localhost)
    at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
    at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
    at scheduleResolveOrReject (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:756:14) [angular]
    at resolvePromise (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:705:21) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:758:17) [angular]
    at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4322:37) [angular]
    at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
    at <anonymous> [<root>]
    at ____________________Elapsed_4_ms__At__Fri_May_05_2017_02_45_19_GMT_0300__BRT_ (http://localhost)
    at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
    at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
    at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
    at scheduleResolveOrReject (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:756:14) [angular]
    at ZoneAwarePromise.then (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:845:17) [angular]
    at new ApplicationInitStatus (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3120:60) [angular]
    at AppModuleInjector.createInternal (ng:///AppModule/module.ngfactory.js:167:35) [angular]
    at AppModuleInjector.NgModuleInjector.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3743:76) [angular]
    at NgModuleFactory.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3716:18) [angular]
    at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4954:61) [angular]
    at Object.onInvoke (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4331:37) [angular]
    at NgZone.run (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4200:62) [<root>]
    at PlatformRef_._bootstrapModuleFactoryWithZone (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4952:23) [<root>]
    at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4993:59) [<root>]
    at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:758:57) [<root>]
    at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
    at <anonymous> [<root>]
defaultErrorLogger @ core.es5.js?0445:1290
ErrorHandler.handleError @ core.es5.js?0445:1350
next @ core.es5.js?0445:4960
schedulerFn @ core.es5.js?0445:4034
SafeSubscriber.__tryOrUnsub @ VM28777:236
SafeSubscriber.next @ VM28777:185
Subscriber._next @ VM28777:125
Subscriber.next @ VM28777:89
Subject.next @ VM28804:55
EventEmitter.emit @ core.es5.js?0445:4020
NgZone.triggerError @ core.es5.js?0445:4391
onHandleError @ core.es5.js?0445:4352
ZoneDelegate.handleError @ zone.js?fad3:369
Zone.runGuarded @ zone.js?fad3:141
_loop_1 @ zone.js?fad3:602
drainMicroTaskQueue @ zone.js?fad3:611
ZoneTask.invoke @ zone.js?fad3:464
person.component.ts?10bd:76 '

Added! Response {_body: null, status: 204, ok: true, statusText: "No Content", headers: Headers…}

请注意,在控制台消息的末尾,我已经记录了消息 'Added!'response 这很好.

那么,为什么我不需要 stringfy 我的 Person object,甚至不需要设置 Content-Typeapplication/json 值,但 REST 端点可以理解它应该只消耗 application/json内容?

有什么想法吗?我错过了什么?

如果你追查http源码

detectContentTypeFromBody方法会为你做内容设置

as detectContentType 方法 default switch

内的案例

https://github.com/angular/angular/blob/master/packages/http/src/static_request.ts#L135