Home > other >  WSO2 APIM localhost rest api get, post, delete,put do not work with json db file
WSO2 APIM localhost rest api get, post, delete,put do not work with json db file

Time:11-21

I have a simple java rest api with a mysql server. I need to deploy this on WSO2 APIM. I used mocky to make a fake rest api but the only call I can make is /songs to get all songs. /songs/6 will not give song with id 6. Same for put, delete, post. I don't know how to approach this. Do I need to host it on a public domain for it to work or are there workarounds? Would love some help here. I also have been trying to create a rest api with my existing java code and Integration studio. I am able to deploy but without much luck. I have also tried creating a react rest api but this approach is effectively no different and does not work either. i'm able to do localhost:3000/songs & localhost:3000/songs/1 to get the song with id 1. But when this same json list used in wso apim i can only do a simple getall request to get the entire list.

APIControllers.class

package com.music.app.rest.Controller;

import com.music.app.rest.Models.Song;
import com.music.app.rest.Repo.SongRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class ApiControllers {
    @Autowired
    private SongRepo songRepo;

    @GetMapping(value = "/")
    public String getPage(){
        return "Welcome";
    }

    @GetMapping(value="/songs")
    public List<Song> getSongs(){
        return songRepo.findAll();
    }
   
    @GetMapping(value = "/song/{id}")
    public Optional<Song> getSongById(@PathVariable long id){
        Song searchSong = songRepo.findById(id).get();
        return songRepo.findById(searchSong.getId());
    }
    @PostMapping(value="/save")
    public String saveSong(@RequestBody Song song){
        songRepo.save(song);
        return "Saved...";
    }
    @PutMapping(value="update/{id}")
    public String updateSong(@PathVariable long id, @RequestBody Song song){
        Song updatedSong = songRepo.findById(id).get();
        updatedSong.setTitle(song.getTitle());
        updatedSong.setArtist(song.getArtist());
        updatedSong.setYear(song.getYear());
        songRepo.save(updatedSong);
        return "Updated...";
    }
    @DeleteMapping(value="delete/{id}")
    public String deleteSong(@PathVariable long id){
        Song deletedSong = songRepo.findById(id).get();
        songRepo.delete((deletedSong));
        return "Deleted song with : "   id   "title: "   deletedSong.getTitle()   " and artist: "   deletedSong.getArtist()   " release in year: "   deletedSong.getYear();
    }

}

json file

{
  "songs": [
    {
      "id": 1,
      "title": "1904",
      "artist": "The Tallest Man on Earth",
      "year": "2012"
    },
    {
      "id": 2,
      "title": "#40",
      "artist": "Dave Matthews",
      "year": "1999"
    },
    {
      "id": 4,
      "title": "#41",
      "artist": "Dave Matthews",
      "year": "1996"
    },
    {
      "id": 5,
      "title": "American Girl",
      "artist": "Tom Petty",
      "year": "1977"
    },
    {
      "id": 6,
      "title": "American Music",
      "artist": "Violent Femmes",
      "year": "1991"
    },
    {
      "id": 7,
      "title": "American Pie",
      "artist": "Don McLean",
      "year": "1972"
    },
    {
      "id": 8,
      "title": "And it Stoned Me",
      "artist": "Van Morrison",
      "year": "1970"
    },
    {
      "id": 9,
      "title": "A Sailor's Christmas",
      "artist": "Jimmy Buffett",
      "year": "1996"
    },
    {
      "id": 10,
      "title": "Badfish",
      "artist": "Sublime",
      "year": "1996"
    },
    {
      "id": 11,
      "title": "Banana Pancakes",
      "artist": "Jack Johnson",
      "year": "2005"
    },
    {
      "id": 12,
      "title": "Barefoot Children",
      "artist": "Jimmy Buffett",
      "year": "1995"
    },
    {
      "id": 13,
      "title": "Big Parade",
      "artist": "The Lumineers",
      "year": "2012"
    },
    {
      "id": 14,
      "title": "Brown Eyed Girl",
      "artist": "Van Morrison",
      "year": "1967"
    },
    {
      "id": 15,
      "title": "Cape Canaveral",
      "artist": "Conor Oberst",
      "year": "2008"
    },
    {
      "id": 16,
      "title": "Carry On",
      "artist": "fun.",
      "year": "2012"
    },
    {
      "id": 17,
      "title": "Catch the Wind",
      "artist": "Donovan",
      "year": "1965"
    },
    {
      "id": 18,
      "title": "Cat's in the Cradle",
      "artist": "Harry Chapin",
      "year": "1974"
    },
    {
      "id": 19,
      "title": "Changes in Latitudes, Changes in Attitudes",
      "artist": "Jimmy Buffett",
      "year": "1977"
    },
    {
      "id": 20,
      "title": "Classy Lumineers",
      "year": "2012"
    },
    {
      "id": 21,
      "title": "Creep",
      "artist": "Radiohead",
      "year": "1993"
    },
    {
      "id": 22,
      "title": "Danny Boy",
      "artist": "Johnny Cash",
      "year": "2002"
    },
    {
      "id": 23,
      "title": "Darkness Between the Fireflies",
      "artist": "Mason Jennings",
      "year": "1997"
    },
    {
      "id": 24,
      "title": "Dead Sea",
      "artist": "The Lumineers",
      "year": "2012"
    },
    {
      "id": 25,
      "title": "Distantly in Love",
      "artist": "Jimmy Buffett",
      "year": "1983"
    },
    {
      "id": 26,
      "title": "Don't Leave Me (Ne Me Quitte Pas)",
      "artist": "Regina Spektor",
      "year": "2012"
    },
    {
      "id": 27,
      "title": "Don't Look Back in Anger",
      "artist": "Oasis",
      "year": "1996"
    },
    {
      "id": 28,
      "title": "Don't Stop Believin'",
      "artist": "Journey",
      "year": "1981"
    },
    {
      "id": 29,
      "title": "Doomsday",
      "artist": "Elvis Perkins",
      "year": "2009"
    },
    {
      "id": 30,
      "title": "Do You Remember",
      "artist": "Jack Johnson",
      "year": "2005"
    },
    {
      "id": 31,
      "title": "Drink the Water",
      "artist": "Jack Johnson",
      "year": "2001"
    },
    {
      "id": 32,
      "title": "Emmylou",
      "artist": "First Aid Kit",
      "year": "2012"
    },
    {
      "id": 33,
      "title": "Fall Line",
      "artist": "Jack Johnson",
      "year": "2003"
    },
    {
      "id": 34,
      "title": "Father and Son",
      "artist": "Cat Stevens",
      "year": "1970"
    },
    {
      "id": 35,
      "title": "Flake",
      "artist": "Jack Johnson",
      "year": "2001"
    },
    {
      "id": 36,
      "title": "Flapper Girl",
      "artist": "The Lumineers",
      "year": "2012"
    },
    {
      "id": 37,
      "title": "Flowers in Your Hair",
      "artist": "The Lumineers",
      "year": "2012"
    },
    {
      "id": 38,
      "title": "Folsom Prison Blues",
      "artist": "Johnny Cash",
      "year": "1957"
    },
    {
      "id": 39,
      "title": "Free Fallin'",
      "artist": "Tom Petty",
      "year": "1989"
    },
    {
      "id": 40,
      "title": "Furr",
      "artist": "Blitzen Trapper",
      "year": "2008"
    },
    {
      "id": 41,
      "title": "Get Well Cards",
      "artist": "Conor Oberst",
      "year": "2008"
    },
    {
      "id": 42,
      "title": "Gulf Coast Highway",
      "artist": "Emmylou Harris",
      "year": "2000"
    },
    {
      "id": 43,
      "title": "Half Light I",
      "artist": "Arcade Fire",
      "year": "2010"
    },
    {
      "id": 44,
      "title": "Half Light II (No Celebration)",
      "artist": "Arcade Fire",
      "year": "2010"
    },
    {
      "id": 45,
      "title": "Harvest",
      "artist": "Neil Young",
      "year": "1972"
    },
    {
      "id": 46,
      "title": "Heart of Gold",
      "artist": "Neil Young",
      "year": "1972"
    },
    {
      "id": 47,
      "title": "here i go again",
      "artist": "whitesnake",
      "year": "1982"
    },
    {
      "id": 48,
      "title": "Hey Jealousy",
      "artist": "Gin Blossoms",
      "year": "1992"
    },
    {
      "id": 49,
      "title": "Hey Soul Sister",
      "artist": "Train",
      "year": "2009"
    },
    {
      "id": 50,
      "title": "High and Dry",
      "artist": "Radiohead",
      "year": "1995"
    },
    {
      "id": 51,
      "title": "Ho Hey",
      "artist": "The Lumineers",
      "year": "2012"
    },
    {
      "id": 52,
      "title": "Hollywood Forever Cemetery Sings",
      "artist": "Father John Misty",
      "year": "2012"
    },
    {
      "id": 53,
      "title": "Home",
      "artist": "Edward Sharpe & The Magnetic Zeros",
      "year": "2009"
    },
    {
      "id": 54,
      "title": "Honey Do",
      "artist": "Jimmy Buffett",
      "year": "1983"
    },
    {
      "id": 55,
      "title": "Hospitals and Jails",
      "artist": "Mason Jennings",
      "year": "2002"
    },
    {
      "id": 56,
      "title": "Hotel California",
      "artist": "The Eagles",
      "year": "1977"
    },
    {
      "id": 57,
      "title": "Hotel Yorba",
      "artist": "The White Stripes",
      "year": "2001"
    },
    {
      "id": 58,
      "title": "I Feel Home",
      "artist": "OAR",
      "year": "1999"
    },
    {
      "id": 59,
      "title": "I Knew You Were Trouble",
      "artist": "Taylor Swift",
      "year": "2012"
    },
    {
      "id": 60,
      "title": "I'm Writing a Novel",
      "artist": "Father John Misty",
      "year": "2012"
    },
    {
      "id": 61,
      "title": "Island in the Sun",
      "artist": "Weezer",
      "year": "2001"
    },
    {
      "id": 62,
      "title": "I Mraz",
      "year": "2012"
    },
    {
      "id": 63,
      "title": "Jack & Diane",
      "artist": "John Mellencamp",
      "year": "1982"
    },
    {
      "id": 64,
      "title": "Karma Police",
      "artist": "Radiohead",
      "year": "1997"
    },
    {
      "id": 65,
      "title": "King of Spain",
      "artist": "The Tallest Man on Earth",
      "year": "2010"
    },
    {
      "id": 66,
      "title": "King of the World",
      "artist": "First Aid Kit",
      "year": "2012"
    },
    {
      "id": 67,
      "title": "Lean On Me",
      "artist": "Bill Withers",
      "year": "1972"
    },
    {
      "id": 68,
      "title": "Little Talks",
      "artist": "Of Monsters and Men",
      "year": "2012"
    },
    {
      "id": 69,
      "title": "Live and Die",
      "artist": "The Avett Brothers",
      "year": "2012"
    },
    {
      "id": 70,
      "title": "Lola",
      "artist": "The Kinks",
      "year": "1970"
    },
    {
      "id": 71,
      "title": "Lonesome Town",
      "artist": "Ricky Nelson",
      "year": "1958"
    },
    {
      "id": 72,
      "title": "Love in the Library",
      "artist": "Jimmy Buffett",
      "year": "1994"
    },
    {
      "id": 73,
      "title": "Love Story",
      "artist": "Taylor Swift",
      "year": "2008"
    },
    {
      "id": 74,
      "title": "Margaritaville",
      "artist": "Jimmy Buffett",
      "year": "1977"
    },
    {
      "id": 75,
      "title": "Me and Julio Down by the Schoolyard",
      "artist": "Paul Simon",
      "year": "1972"
    },
    {
      "id": 76,
      "title": "Migration",
      "artist": "Jimmy Buffett",
      "year": "1974"
    },
    {
      "id": 77,
      "title": "Moonshadow",
      "artist": "Cat Stevens",
      "year": "1971"
    },
    {
      "id": 78,
      "title": "Mudfootball",
      "artist": "Jack Johnson",
      "year": "2001"
    },
    {
      "id": 79,
      "title": "My Antonia",
      "artist": "Emmylou Harris",
      "year": "2000"
    },
    {
      "id": 80,
      "title": "New Realization",
      "artist": "Sublime",
      "year": "1996"
    },
    {
      "id": 81,
      "title": "No Surprises",
      "artist": "Radiohead",
      "year": "1997"
    },
    {
      "id": 82,
      "title": "Nothing",
      "artist": "Mason Jennings",
      "year": "1997"
    },
    {
      "id": 83,
      "title": "Nothing Else Matters",
      "artist": "Metallica",
      "year": "1992"
    },
    {
      "id": 84,
      "title": "Only Son of the Ladiesman",
      "artist": "Father John Misty",
      "year": "2012"
    },
    {
      "id": 85,
      "title": "Out on the Weekend",
      "artist": "Neil Young",
      "year": "1972"
    },
    {
      "id": 86,
      "title": "Party Cyrus",
      "year": "2009"
    },
    {
      "id": 87,
      "title": "Patience",
      "artist": "Guns N' Roses",
      "year": "1989"
    },
    {
      "id": 88,
      "title": "Redemption Song",
      "artist": "Bob Marley",
      "year": "1980"
    },
    {
      "id": 89,
      "title": "Rivers of Babylon",
      "artist": "Sublime",
      "year": "1998"
    },
    {
      "id": 90,
      "title": "Rocket Man",
      "artist": "Elton John",
      "year": "1972"
    },
    {
      "id": 91,
      "title": "Rodeo Clowns",
      "artist": "JackJohnson",
      "year": "2003"
    },
    {
      "id": 92,
      "title": "Send My Fond Regards to Lonelyville",
      "artist": "Elvis Perkins",
      "year": "2009"
    },
    {
      "id": 93,
      "title": "Sentimental Heart",
      "artist": "She & Him",
      "year": "2008",
      "web_url": "http://www.songnotes.cc/songs/109-she-and-him-volume-one",
      "img_url": "http://fireflygrove.com/songnotes/images/artists/SheAndHim.jpg"
    },
    {
      "id": 94,
      "title": "Shelter from the Storm",
      "artist": "Bob Dylan",
      "year": "1975"
    },
    {
      "id": 95,
      "title": "Some Nights",
      "artist": "fun.",
      "year": "2012"
    },
    {
      "id": 96,
      "title": "Somewhere Only We Know",
      "artist": "Keane",
      "year": "2004"
    },
    {
      "id": 97,
      "title": "Space Oddity",
      "artist": "David Bowie",
      "year": "1969"
    },
    {
      "id": 98,
      "title": "Stay or Leave",
      "artist": "Dave Matthews",
      "year": "2003"
    },
    {
      "id": 99,
      "title": "Stubborn Love",
      "artist": "The Lumineers",
      "year": "2012"
    },
    {
      "id": 100,
      "title": "Stuck in the Middle With You",
      "artist": "Stealers Wheel",
      "year": "1972"
    },
    {
      "id": 101,
      "title": "test",
      "artist": "test",
      "year": "2012"
    }
  ]
}

CodePudding user response:

If some resources work and the rest won't, there should e a problem with your backend or your request.

  1. Can you check whether the backend works individually for each request and respond with the expected response?

  2. Check the request that you invoke. As per your implementation, @GetMapping(value = "/song/{id}"), you should invoke the https://localhost:8243/basepath/song/1. Invoking /songs/6 will not work.

CodePudding user response:

In order not to have this problem, add the Swagger / OpenAPI documentation to your SpringBoot API, this will generate the Swagger documentation that you just import into WSO2, with this, you will not make the resource mappings wrong.

If it works on the backend it will work on WSO2.

https://www.baeldung.com/spring-rest-openapi-documentation

  • Related