0

I have a problem of API Restfull backend (jersey jpa) and frontend (angular 5) in my development because when I execute an update operation of my application, I have never encountered this problem before, so I'm not able to understand this. code here

title: LangueRest.java 
package com.spro.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.spro.dao.DaoFactory;
import com.spro.entity.Langue;
import com.spro.jpa.JpaLangueDao;


@Path("/langue")
public class LangueRest {

    private JpaLangueDao langueDao;

    public LangueRest() {
        this.langueDao = DaoFactory.getInstance().getLangueDao();
    }

    @Path("/getall")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Langue> getAll(){
        return langueDao.findAll();
    }

    @Path("/{id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Langue getById(@PathParam("id") long id){
        return langueDao.findById(id).get(0);
    }

    @Path("/add")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void add(Langue langue) {
        langueDao.add(langue);
    }

    @Path("/update")
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public void update(Langue langue) {
        langueDao.update(langue);
    }

    @Path("/delete")
    @
    @Consumes(MediaType.APPLICATION_JSON)
    public void delete(Langue langue) {
        langueDao.delete(langue);
    }
}

langue.service.ts

import { Langue } from './langue';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Observable} from 'rxjs';
import 'rxjs/add/operator/map';

const httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json'})
};

@Injectable()
export class LangueService {

  constructor(private http: HttpClient) {}
  // lister toutes les Langues
  getLangues() {
    return this.http.get('http://localhost:8081/APIRest/resources/langue/getall');
  }
  // créer un enregistrement langue
  createLangue(langue:Langue) {
    return this.http.post('http://localhost:8081/APIRest/resources/langue/add', JSON.stringify(langue), httpOptions);
  }
  // modifier une langue
  updateLangue(langue:Langue) {
    alert("sssssss "+ httpOptions);
    return this.http.put('http://localhost:8081/APIRest/resources/langue/update' + langue.langueId, JSON.stringify(langue), httpOptions);
  }
  // supprimer une langue
  deleteLangue(langue:Langue) {
  return this.http.delete('http://localhost:8081/APIRest/resources/langue/delete/'+ langue.langueId);
  }
}

the error is:

PUT http://localhost:8081/APIRest/resources/langue/update3092 405 (Non Allowed Method)

Vikas
  • 11,859
  • 7
  • 45
  • 69

1 Answers1

0

Look at how you are building the URL. You don't even have a slash between the 'update' and the id. So you need to fix that. You are getting the 405 because there is a path @Path("{id}"), where update3092 is considered the id.

After you fix this with the client, you will then get a 404. What you need is an endpoint that can handle @Path("update/{id}") if you want to send the id path parameter

@Path("/update/{id}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void update(@PathParam("id") long id, Langue langue) {
    Language inStore = langueDao.findById(id);
    if (inStore == null) {
        throw new NotFoundException();
    }
    langueDao.update(langue);
}

After you add this endpoint, then you might still have a CORS problem. If you do, see this post

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720