AnonymousAlias
AnonymousAlias

Reputation: 1399

REST API with JAX-RX sending and receiving parsed JSON from form in jsp page

I am trying to create a form in the jsp page of my project that will send the information needed to create a CD object. I know this works with JSON as when I use a Google plugin called postman I can make a post request with json and it adds the new cd to the list of all cds.

Although I need to make a GUI client that will work with json but display the information parsed. I will post a few of the classes. I have searched the site but cant find anything that can really help me here. I am getting an unsupported media type error... i guess because i need to parse to json somehow before sending the data?

@Path("/cd")
public class CDResources {

    CDService c = new CDService();
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public CD addCD(CD cd)
    {
        return c.addCD(cd);
    }

Here is the form

<form action="webapi/cd/" method="post">
        <p>
            Artist : <input type="text" name="artist" />
        </p>

                <p>
            Artist : <input type="number" name="numberOfTracks" />
        </p>
                <p>
            Artist : <input type="text" name="title" />
        </p>

        <input type="submit" value="Submit" />
    </form>

CD class

package Assignment.crud.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class CD {

    String title;
    int numberOfTracks;
    String artist;
    long id;


    public CD() {

    }
    public CD(long id,String title,String artist,int numberOfTracks) {
        super();
        this.title = title;
        this.numberOfTracks = numberOfTracks;
        this.artist = artist;
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getNumberOfTracks() {
        return numberOfTracks;
    }
    public void setNumberOfTracks(int numberOfTracks) {
        this.numberOfTracks = numberOfTracks;
    }
    public String getArtist() {
        return artist;
    }
    public void setArtist(String artist) {
        this.artist = artist;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }


}

CD service class

package Assignment.crud.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import Assignment.crud.database.DatabaseClass;
import Assignment.crud.model.CD;

public class CDService {

    private Map<Long, CD> cds = DatabaseClass.getCDs();

    public CDService()
    {
        cds.put(1L, new CD(1,"Fleet Foxes","Helplessness Blues",10));
        cds.put(2L, new CD(2,"Van Morrison","Tupelo Honey",12));

    }
    public List<CD> getAllCDs()
    {   
        return new ArrayList<CD>(cds.values()); 
    }

    public CD getCD(long id)
    {   
        return cds.get(id); 
    }

    public CD addCD(CD cd)
    {   
        cd.setId(cds.size()+1); 
        cds.put(cd.getId(), cd);
        return cd;
    }

    public CD updateCD(CD cd)
    {   
        if(cd.getId()<=0)
        {
            return null;
        }
        cds.put(cd.getId(), cd);
        return cd;
    }

    public CD removeCD(long id)
    {
        return cds.remove(id);
    }

}

Upvotes: 3

Views: 299

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209072

HTML forms (unless you are using AJAX) will send data in application/x-www-form-urlencoded data type. So you need to fix your resource method to accept this type

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public CD addCD(@FormParam("artist") String artist,
                @FormParam("title") String title,
                @FormParam("numberOfTracks") int numberOfTracks) {

}

You might also want to send a redirect to follow the Post/Redirect/Get pattern.

public Response addCD(...) {
    return Response.seeOther(...someUrl).build()
}

Also you might want to consider using some AJAX library to make the request, instead of using basic HTML forms. This is how a REST API is supposed to work.

Upvotes: 3

Related Questions