How to get a POST parameter in REST application

Asked

Viewed 1,260 times

2

I started a Rest application and have a java class createPost

    POST
    @Path("/post/")
    @Produces(MediaType.APPLICATION_JSON)
    public static String createPost(@FormParam("loopID") String loopID) throws IOException {
         ConnectDAO dao = ConnectDAO();
         Post post = new Post();
         post.setLoopId(loopID);
    }

Where it will receive by parameter this "value".

In javascript I have to create a service since I use angular called createPost, and it will do the "POST".

createPost: function(loopID) {
            if (!loopID) {
                hyatt.ui.view.alert('There is no file to upload. Please choose a file.');
            } else {
                return http({
                    method: 'POST',
                    url: url + '/post/',
                    headers: {
                        'Content-Type': undefined
                    },
                    transformRequest: formData
                });
            }
            // This forces the break of execution.
            throw new Error('The UploadService service needs a file and this file cannot be a video');


        },

Above I’m getting by parameter the value I want to pass to JAVA.How can I be doing this? I tried with @Formparam but it didn’t work.

Note: I am not currently able to pick up via @Pathparam since the url calls an external service.

Anyone who can help, thank you.

1 answer

1


Try it like this.

@POST
@Path("/post/")
@Produces("application/json ; charset=UTF-8")
public String createPost(String loopId){
    ConnectDAO dao = ConnectDAO();
    Post post = new Post();
    post.setLoopId(loopID);
    return "";
}
  • restored to me the following error: 204 - No Content. I believe I have solved in the sense of taking the parameter...

  • You debugged the java code to see if the method was executed and if the loopId parameter was filled in?

  • yes! de qualquer forma consegui assim no JS
var data = '&idLoop=' + idLoop + '&title=' + title + '&folderID=' + folderID;
 return http.post(urlYapmo + '/post/' +
 hyatt.date.getParameterTimeStamp(), data);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.