为什么我不需要JSON.stringify()这个帖子有效载荷?

时间:2017-05-05 06:01:10

标签: json rest angular post resteasy

我读过:

How to post json object with Http.post (Angular 2) (php server side)

How do I POST JSON in Angular 2?

两者都建议在执行POST请求之前创建头文件并将我的对象指向JSON,这有效,但我注意到如果我只是传递对象它也可以。

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

我正在使用:

  • Angular 4前端。
  • Wildfly 10.1.0 final
  • Resteasy 3.1.1 final

我使用以下方法 person.component.ts

addPerson() {
    this.personService.save(this.person)
       .then(result => {
        console.log('Added!', result);
       });
  }
使用保存方法

person.service.ts

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

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

@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);
    }
    ...

我实际上在前端获得了一个例外,但我的记录得到持久到数据库。我认为异常与我正在使用的按钮或屏幕上的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…}

请注意,在控制台消息的末尾,我已记录消息'已添加!'响应,这很好。

再次,为什么我不需要 stringfy 我的 Person 对象,甚至不将 Content-Type 设置为 application / json 值但REST端点应该理解它应该只使用 application / json 内容吗?

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

1 个答案:

答案 0 :(得分:1)

如果您追查http源代码

detectContentTypeFromBody方法将为您进行内容设置

detectContentType

中的default方法switch个案

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