0

Essentially I have a front end using AngularJS and a back end using Java. I need to be able to send a value from the front end via an input box to the back end to make use of in the Java.

I found a basic example, namely:

Java

package com.mkyong.rest;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/user")
public class UserService {

@POST
@Path("/add")
public Response addUser(@FormParam("name") String name,
        @FormParam("age") int age) {

    return Response.status(200)
            .entity("addUser is called, name : " + name    + ", age : " + age)
            .build();

    }
}

HTML

<html>
<body>
<h1>JAX-RS @FormQuery Testing</h1>

<form action="rest/user/add" method="post" >
<p>Name : <input type="text" name="name" /></p>
<p>Age : <input type="text" name="age" /></p>
<input type="submit" value="Add User" />
</form>

</body>
</html>

I understand the @FormParam parts and how that works but what I don't understand is how the @Path works as well as the @POST. Is this linked to the form action and is it that simple or is there some step in between?

A general explanation of how this works/if my assertions are correct would be appreciated.

2 Answers 2

1

What i got from your question you want to more about @POST and @PATH if yes. than @POST is tell you the type of request like post, get, put, delete etc. that means your addUser method call only when POST request come to server but path should correct. Now @PATH tells you about the URL to call addUser method, In your case @Path("/user") public class UserService means whenever a request come to /user it call you UserService class, further if we have multiple method in same class like adduser, deleteuser, getuser than you need to tell server which method call for URL so we put @PATH on method and our URL is form. Example

@POST
@Path("/add")
public Response addUser
//URL for aboue method is <baseURL>/user/add
@POST
@Path("/delete")
public Response deleteUser
//URL for above method is <baseURL>/user/delete

I hope you under stand the URL mapping in webservice, you need more study about URL mapping in web service.

About <baseURL> is also depende upon the web.xml configuration.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helps a lot
0

Couple of things to note here

1) URL in form action is wrong . It should be /user/add to

2) You dont have any JS file that mentions about the angular application .

To start a application we need to have something like shown below

   var app = angular.module('app',[ 'ngRoute','ngResource']);

1 Comment

URL also depends on web.xml configuration, we can not say it is wrong until we saw the web.xml file. And your second point is valide, but he want to more input on @PATH and @POT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.