我正在尝试向我的Java Rest服务器发送一个发布请求,它可以在10个URL上正常工作,但是由于某种原因,我在提供的URL上收到错误404,并在Fiddler中将其检出,并且可以正常工作精细。 我的目标是能够将JSON对象发送到服务器,但由于某些原因我不断收到错误404。
这是网址无效的服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Coupon } from '../Entities/coupon';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CompanyService {
private CrtCouponURL = 'http://localhost:8080/CouponSystemWeb/rest/company/CrtCoupon'
private RmvCouponURL ='http://localhost:8080/CouponSystemWeb/rest/company/RmvCoupon'
private UpdtCouponURL ='http://localhost:8080/CouponSystemWeb/rest/company/UpdtCoupon'
private GetAllCouponURL ='http://localhost:8080/CouponSystemWeb/rest/company/GetAllCoupons'
private couponByTypeURL ='http://localhost:8080/CouponSystemWeb/rest/company/TypeCouponsGet'
constructor(private http:HttpClient){ }
public createCoupon(coupon:Coupon):Observable<Coupon>{
return this.http.post<Coupon>(this.CrtCouponURL, coupon ,{withCredentials:true})
}
public deleteCoupon(id:number):Observable<Coupon>{
return this.http.delete<Coupon>(this.RmvCouponURL+'/?id='+id,{withCredentials:true})
}
public updateCoupon(coupon:Coupon):Observable<Coupon>{
return this.http.put<Coupon>(this.UpdtCouponURL,coupon,{withCredentials:true})
}
public getAllCoupons():Observable<Coupon[]>{
return this.http.get<Coupon[]>(this.GetAllCouponURL,{withCredentials:true})
}
public getAllCouponsByType(type:string):Observable<Coupon[]>{
return this.http.get<Coupon[]>(this.couponByTypeURL+'/?type='+type,{withCredentials:true})
}
}
这是具有有效网址的服务:
@Injectable({
providedIn: 'root'
})
export class AdminService {
private companyUrl = 'http://localhost:8080/CouponSystemWeb/rest/admin/company'
private customerUrl = 'http://localhost:8080/CouponSystemWeb/rest/admin/customer'
private companyCrtUrl = 'http://localhost:8080/CouponSystemWeb/rest/admin/companyCrt'
private updateCompURL = 'http://localhost:8080/CouponSystemWeb/rest/admin/UpdateComp'
private customerCrtUrl = 'http://localhost:8080/CouponSystemWeb/rest/admin/CreateCustomer'
private customerDelUrl = 'http://localhost:8080/CouponSystemWeb/rest/admin/RmvCustomer'
constructor(private http: HttpClient) { }
public createCompany(company: Company): Observable<Company> {
return this.http.post<Company>(this.companyCrtUrl, company, { withCredentials: true })
}
public deleteCompany(id: number): Observable<Company> {
return this.http.delete<Company>("http://localhost:8080/CouponSystemWeb/rest/admin/mycompany/" + id, { withCredentials: true })
}
public updateCompany(id: number, company: Company): Observable<Company> {
company.id = id;
return this.http.put<Company>(this.updateCompURL, company, { withCredentials: true })
}
public getCompany(id: number): Observable<Company> {
return this.http.get<Company>(this.companyUrl + "?=id" + id, { withCredentials: true })
}
public getAllCompanies(): Observable<Company[]> {
return this.http.get<Company[]>(this.companyUrl, { withCredentials: true })
}
public createCustomer(customer: Customer): Observable<Customer> {
return this.http.post<Customer>(this.customerCrtUrl, customer, { withCredentials: true })
}
public deleteCustomer(id: number): Observable<Customer> {
return this.http.delete<Customer>(this.customerDelUrl + '?id=' + id, { withCredentials: true })
}
public updateCustomer(id: number,customer: Customer): Observable<Customer> {
return this.http.put<Customer>('http://localhost:8080/CouponSystemWeb/rest/admin/UpdtCustomer', customer, { withCredentials: true })
}
public getAllCustomer(): Observable<Customer[]> {
return this.http.get<Customer[]>(this.customerUrl, { withCredentials: true })
}
}
几点: 我没有在数据库中使用SQL。 我不使用:InMemoryDataService。
更新:它与Postman都不起作用。
后端代码:
@Path("company")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CompanyService {
@Context
private HttpServletRequest requst;
@POST
@Path(" couponCrt")
public Response createCoupon(Coupon coupon) {
HttpSession session = requst.getSession(false);
CompanyFacade companyFacade = (CompanyFacade) session.getAttribute("companyFacade");
try {
companyFacade.createCoupon(coupon);
return Response.status(Status.CREATED).type(MediaType.APPLICATION_JSON).build();
} catch (Exception e) {
throw new CouponSystemWebExeption("Error while creating Coupon : " + coupon.getTitle());
}
}
答案 0 :(得分:0)
对于您的帖子或放置请求,您需要先将其转换为如下所示的json字符串
public createCustomer(customer: Customer): Observable<Customer> {
let body = JSON.stringify(customer);
return this.http.post<Customer>(this.customerCrtUrl, body, { withCredentials: true })
}
对于404错误,您可以在浏览器中打开任何获取URL来检查其是否正常工作吗?