我有当前的服务类,该类提供针对角度应用程序的GET,POST和DELETE请求:
@Path("Trips")
public class TripREST {
...
...
@POST
@Path("Trip")
@Produces("application/json")
@Consumes("application/json")
@Transactional
public Response addTrip(Trip trip) {
tripDao.addTrip(trip);
return Response.ok().entity("Trip added successfully").build();
}
...
@GET
@Path("Names/{tripId}")
@Produces("application/json")
public List<Names> loadNames(@PathParam("tripId") String tripId) {
List<Names> names = tripDao.getNames(triplId);
return names;
}
...
@DELETE
@Path("Trip/Delete/{id}")
public Response deleteTrip(@PathParam("id") String id) {
tripDao.deleteTrip(Integer.valueOf(id));
return Response.ok().entity("Trip deleted successfully").build();
}
...
}
我正在尝试从浏览器的url运行该服务以进行测试,POST和其他GET方法工作正常,但DELETE不起作用,也没有错误。
让url工作正常:
http://localhost:8080/jboss-seam-jpa/seam/resource/rest/Trips/Names/10000000
无效的删除网址是:
http://localhost:8080/jboss-seam-jpa/seam/resource/rest/Trips/Trip/Delete/13
我得到的是“找不到页面”错误,我里面有调试点 服务方法,没有钩子。
这是来自角度应用程序的服务呼叫:
const httpOptions = {
headers: new HttpHeaders({ 'Accept': 'text/plain', 'Content-Type': 'text/plain' })
};
...
...
deleteTrip(id: string) {
return this.http.delete(`${this.baseUrl}/Trips/Trip/Delete/${id}`, httpOptions);
}
更新: 我已经使用POSTMAN测试了该服务,发现其余api正常运行。因此问题出在维修服务电话上。 我意识到我没有订阅,所以我将通话更改为:
deleteTrip(id: string) {
return this.http.delete(`${this.baseUrl}/Trips/Trip/Delete/${id}`, httpOptions).subscribe();
}
我在chrome浏览器控制台中看到的是以下错误消息:
zone.js:2935 OPTIONS http://localhost:8080/jboss-seam-jpa/seam/resource/rest/Funds/Instrument/Delete/12 405 (Method Not Allowed)
scheduleTask @ zone.js:2935
ZoneDelegate.scheduleTask @ zone.js:407
onScheduleTask @ zone.js:297
ZoneDelegate.scheduleTask @ zone.js:401
Zone.scheduleTask @ zone.js:232
Zone.scheduleMacroTask @ zone.js:255
scheduleMacroTaskWithCurrentZone @ zone.js:1092
(anonymous) @ zone.js:2967
proto.(anonymous function) @ zone.js:1372
(anonymous) @ http.js:2366
Observable._trySubscribe @ Observable.js:172
Observable.subscribe @ Observable.js:160
subscribeToResult @ subscribeToResult.js:23
MergeMapSubscriber._innerSub @ mergeMap.js:138
MergeMapSubscriber._tryNext @ mergeMap.js:135
MergeMapSubscriber._next @ mergeMap.js:118
Subscriber.next @ Subscriber.js:92
ScalarObservable._subscribe @ ScalarObservable.js:51
Observable._trySubscribe @ Observable.js:172
Observable.subscribe @ Observable.js:160
MergeMapOperator.call @ mergeMap.js:92
Observable.subscribe @ Observable.js:157
MaterializeOperator.call @ materialize.js:64
Observable.subscribe @ Observable.js:157
DelayOperator.call @ delay.js:66
Observable.subscribe @ Observable.js:157
DeMaterializeOperator.call @ dematerialize.js:59
Observable.subscribe @ Observable.js:157
subscribeToResult @ subscribeToResult.js:23
MergeMapSubscriber._innerSub @ mergeMap.js:138
MergeMapSubscriber._tryNext @ mergeMap.js:135
MergeMapSubscriber._next @ mergeMap.js:118
Subscriber.next @ Subscriber.js:92
ScalarObservable._subscribe @ ScalarObservable.js:51
Observable._trySubscribe @ Observable.js:172
Observable.subscribe @ Observable.js:160
MergeMapOperator.call @ mergeMap.js:92
Observable.subscribe @ Observable.js:157
FilterOperator.call @ filter.js:61
Observable.subscribe @ Observable.js:157
MapOperator.call @ map.js:57
Observable.subscribe @ Observable.js:157
InstrumentsComponent.deleteFundInstrument @ instruments.component.ts:127
(anonymous) @ InstrumentsComponent.html:139
handleEvent @ core.js:13547
callWithDebugContext @ core.js:15056
debugHandleEvent @ core.js:14643
dispatchEvent @ core.js:9962
(anonymous) @ core.js:10587
(anonymous) @ platform-browser.js:2628
ZoneDelegate.invokeTask @ zone.js:421
onInvokeTask @ core.js:4740
ZoneDelegate.invokeTask @ zone.js:420
Zone.runTask @ zone.js:188
ZoneTask.invokeTask @ zone.js:496
invokeTask @ zone.js:1517
globalZoneAwareCallback @ zone.js:1543
30056021:1 Failed to load http://localhost:8080/jboss-seam-jpa/seam/resource/rest/Trips/Trip/Delete/12: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
Cors过滤器tomcat 7配置:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Content-Type, Accept, Authorization</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>