java - How to add parameters in rest api for specific response in code? -


i have created first basic rest api. working find , give me output in xml using url

http://localhost:8080/webservice/test/restclient 

now, i'm thinking if want api ask server specific data how can use parameters same in api url.

here little updated code

package restclient;  //import javax.servlet.annotation.webservlet; import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.pathparam; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; import javax.xml.bind.annotation.xmlrootelement;  @path("/restclient") @xmlrootelement public class restwebclient {      @get     @produces(mediatype.text_xml)     public string sayxmlhello(@pathparam("x") int x, @pathparam("y") int y) {          string output = "";          if (x == 1 && y == 1) {             output = "<?xml version=\"1.0\"?>" + "<hello> hello jersey" + "</hello>";         }          else if (x == 2 && y == 2) {             output = "<?xml version=\"1.0\"?>" + "<hello> hello jersey" + "</hello>";         }          return output;     }  } 

and web.xml here

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">   <display-name>webservice</display-name>   <welcome-file-list>     <welcome-file>index.html</welcome-file>     <welcome-file>index.htm</welcome-file>     <welcome-file>index.jsp</welcome-file>     <welcome-file>default.html</welcome-file>     <welcome-file>default.htm</welcome-file>     <welcome-file>default.jsp</welcome-file>   </welcome-file-list>  <servlet> <servlet-name>java ws</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class>  <init-param>     <param-name>jersey.config.server.provider.packages</param-name>     <param-value>restclient</param-value> </init-param>    <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping>     <servlet-name>java ws</servlet-name>     <url-pattern>/test/*</url-pattern> </servlet-mapping>    </web-app> 

but when run url passing parameters nothing comes in output

http://localhost:8080/webservice/test/restclient?x=1&y=1 

note: expected output above parameters in url should first if condition

may know i'm missing achieve ?

you sending parameters in query should use @queryparam , not @pathparam. change method to:

public string sayxmlhello(@queryparam("x") int x, @queryparam("y") int y) { 

query params:

http://localhost:8080/webservice/test/restclient?x=1&y=1 

path params need change path:

@path("{x}/{y}/restclient")  http://localhost:8080/webservice/test/1/1/restclient 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -