package my.first.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("hello")
public class Hello {
// This method is called if TEXT_PLAIN is requested
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayHelloInPlainText() {
return "Hello world!";
}
http://stackoverflow.com/questions/2893796/create-hello-world-with-restful-web-service-and-jersey
// This method is called if HTML is requested
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHelloInHtml() {
return "<html> " + "<title>" + "Hello world!" + "</title>"
+ "<body><h1>" + "Hello world!" + "</body></h1>" + "</html> ";
}
}
This is my code I wrote web service Restful. When I publish with Test Client Then this error is coming:
IWAB0489E Error when deploying Web service to Axis runtime
axis-admin failed with {http://schemas.xmlsoap.org/soap/envelope/}
Server.userException java.net.ConnectException: Connection refused: connect
How will I fix it? This is coming with Rest only, not with SOAP.
You have to download the jersey [implementation of REST from sunmicrosystem] and include those jars in your application apart from this you have to modify your web.xml accordingly.
There are multiple beginners tutorial available on net - click here
Related
I am trying to send an array of strings using axios to a rest endpoint using jersey with spring boot and tomcat. It results in a 404 and I am clueless. The url being passed is correct because if I send a single string with GET it works correctly.
EDIT: Any exception within any another endpoint (say NullPointerException) also results in 404 being shown in the browser. This is something to do with the configuration. So 404 is kind of a red herring status code.
This is my java side code
import org.springframework.stereotype.Component;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;
#Component
#Path("v1/")
public class SomeResourceV1 {
#POST
#Path("delete")
#Consumes(MediaType.APPLICATION_JSON)
public void deleteFoo(List<String> ids) {
if (ids != null) {
// do something
}
}
}
This is the typescript code:
public delete(someIds : string[]) {
axios({
method: 'POST',
url: "/v1/delete",
data: someIds
}).then((response : any) => {
}).catch((error) => {
console.log("*** delete error ***", error);
});
}
I have also tried to send data as
data:{
ids: someIds
}
I have also attempted to use transformRequest but to no avail.
How does I fix this? Thanks for the help!
am using below code to test a basic web service. When am passing normal string it works fine for example - http://localhost.com:8080/CheckRest/rest/pmg?p1=xyz. It displays HELLO xyz
But when I add '#' to the URL it doesnt give the right output for example - http://localhost.com:8080/CheckRest/rest/pmg?p1=xyz#abc. It then displays HELLO xyz instead of HELLO xyz#abc
package com.check.ws;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
#Path("/pmg")
public class CheckCall {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return " ";
}
// This method is called if XML is request
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<pmg> </pmg>";
}
// This method is called if HTML is request
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(#QueryParam("p1") String par1) {
return "<html> <body> HELLO </body> </html>"+par1;
}
}
The pound/hash sign (#) indicates the beginning of the URL fragment identifier. If you want to use a pound/hash sign in your query string, you need to URL encode it by replacing it with %23:
http://localhost.com:8080/CheckRest/rest/pmg?p1=xyz%23abc
I created a web service rest with jersey-json Java running in tomcat 8.0. My client is a phonegap app with java script and jquery. When I call the rest webservice in browser or soapui it's return ok. When I call with app (phonegap/javascript/jquery) it return server error 500:
{
"code": "ECONNREFUSED",
"errno": "ECONNREFUSED",
"syscall": "connect"
}
Error in chrome console
Following is my source webservice server Java code:
package oneTrade.usuario;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/usuario")
public class UsuarioWS {
#GET
#Path("/listarUsuarios")
#Produces("application/json")
public List<Usuario> listarUsuarios(){
return new UsuarioRN().listar();
}
}
Following is my webservice client javascript code:
function json2(){
$.ajax(
{
dataType: "json",
type: "GET",
url:"http://localhost:8080/oneTrade/rest/usuario/listarUsuarios",
success: function(data){
alert('Sucesso :' + data[0].cpf);
},
error: function(data){
alert('Erro ' + data.responseText + ' ' +data.thrownError+' '+data.status);
}
});
}
From somewhere I got some stupid and invalid URL Request to a Webservice, like
http://localhost:8080/WebApplication1/webresources/testme?stupidparameter=dyndcvgz/&%$\xa7edns
My sample Application looks like following, build with Netbeans on glassfish 4.0 and jdk1.7u40
package WebApplication1;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
#Stateless
#Path("/testme")
public class NewSessionBean {
/**
* http://localhost:8080/WebApplication1/webresources/testme?stupidparameter=dyndcvgz/&%$\xa7edns
*
* #param stupidparameter
* #return
*/
#GET
#Produces("text/plain")
public String getXml(
#QueryParam("stupidparameter") String stupidparameter) {
System.out.println("stupidparameter = " + stupidparameter);
return "got " + stupidparameter;
}
}
and with the given test url I always got IllegalStateException: isHexDigit from org.jboss.weld.servlet.WeldListener. How can I avoid or handle this exception?
This seems like a problem with Glassfish, the way it's processing parameters with special characters.
https://java.net/jira/browse/GRIZZLY-1538
Until the issue is resolved in a future release, you could try to encode the parameter as base64 and decode it back when the paramter is received inside getXml(). This would avoid having related special characters from passing through logic inside glassfish where the exception is thrown.
I know there is a lot of these posts but I couldn't really get this to work since I am new to both REST and JQuery:
I am using REST-WS with Java 5 and I am able to call it and get result back with "Poster" ,the firefox plugin to test it. When I call the URL below I should get the employee in order '0' in the map by calling the method "getCustomer" in the resource class shown below.
Although I am not able to get the result and getting an error "unknown "using jQuery and returning JSON when I call the REST from an html page with body as below:
<body>
jQuery to REST <br><br>
jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function (){
$.ajax({
type: "GET",
url: "http://localhost:8081/RestDemo/services/customers/0",
dataType: "json",
success: function (data) {
alert(data.name);
},
error: function(e){
alert("Error: " + e);
}
});
});
});
</script>
<br>
<br>
<button>Return Customer</button>
</body>
This is my Resource class:
package com.myeclipseide.ws;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.sun.jersey.spi.resource.Singleton;
#Produces("application/xml")
#Path("customers")
#Singleton
#XmlRootElement(name = "customers")
public class CustomerResource {
private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();
public CustomerResource() {
// hardcode a single customer into the database for demonstration
// purposes
Customer customer = new Customer();
customer.setName("Harold Abernathy");
customer.setAddress("Sheffield, UK");
addCustomer(customer);
}
#GET
#XmlElement(name = "customer")
public List<Customer> getCustomers() {
List<Customer> customers = new ArrayList<Customer>();
customers.addAll(customerMap.values());
return customers;
}
#GET
#Path("/{id}")
#Produces("application/json")
public String getCustomer(#PathParam("id") int cId) {
return "{\"name\": \"unknown\", \"address\": -1}"; //customerMap.get(cId);
}
#POST
#Path("add")
#Produces("text/html")
#Consumes("application/xml")
public String addCustomer(Customer customer) {
int id = customerMap.size();
customer.setId(id);
customerMap.put(id, customer);
return "Customer " + customer.getName() + " added with Id " + id;
}
}
I appreciate anyone's help,
Thanks!
I got it!
Returning {"name": "unknown", "address": -1} is right because that's exactly what is hard coded in my method return,
so i replaced return "{\"name\": \"unknown\", \"address\": -1}";
simply with a correct form which is
return "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";
and obviously it works!
Thanks everyone.
I appreciate anyone's help,
If you are stumped, take a look at what is in the web countainer's log files. If necessary, turn on debug logging.
The next thing would be to use your web browser's built-in "web developer" support to see what request is actually being sent.