Reputation: 7648
Iam a new bie in webservice.Please help me.I am trying to pass an object into webresource using Jersey Implementation.But i got error
"HTTP Status 405" and description is "The specified HTTP method is not allowed for the requested resource ()."
I mentioned the below object ,web resouce method,Html page
FruitBean:-
@XmlRootElement(name="fruitbean")
public class FruitBean {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
FruitStore Service:-
@Path("fruitstore")
public class FruitStore {
@PUT
@Path("checkIDByObject")
@Consumes("application/xml")
public void loadObject(FruitBean bean){
System.out.println("====================");
System.out.println("Fruit ID"+bean.getId()+" Name"+bean.getName());
}
}
Index.htm:-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Jax-RS Object</title>
</head>
<body>
<form action="services/fruitstore/checkIDByObject" method="POST">
<table>
<tr>
<td>ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><input type="submit" Value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Iam trying to run this index.htm.But i got exception.How to pass an object into webresource method in Restfull webserice using jersey.Please Help me.
Update :-
FruitStore Service:-
@Path("fruitstore")
public class FruitStore {
@POST
@Path("checkIDByObject")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void loadObject(FruitBean bean){
System.out.println("====================");
System.out.println("Fruit ID"+bean.getId()+" Name"+bean.getName());
}
}
FruitBean:-
@XmlRootElement(name="fruitbean")
public class FruitBean {
private long id;
private String name;
@XmlAttribute
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Index.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Jax-RS Object</title>
</head>
<body>
<form action="services/fruitstore/checkIDByObject" method="POST" enctype="application/x-www-form-urlencoded">
<table>
<tr>
<td>ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><input type="submit" Value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
I got below message in the console
SEVERE: A message body reader for Java type, class com.service.fruitstore.FruitBean, and MIME media type, application/x-www-form-urlencoded, was not found
Please help me
Upvotes: 1
Views: 11938
Reputation: 507
I was having the same problems too. However, I did it a different way.
Instead of passing in an object (which would be easier, however, I am also doing this for learning purposes), I used the @FormParam
annotation.
HTML
<form action="../{projectname}/{rest}/{resource}/findByIdOrName" action="post">
<label>id</label>
<input type="text" name="id" />
<label>name</label>
<input type="text" name="name" />
<input type="submit" value="Find" />
</form>
JAX-RS Resource
@Path("/resource")
public class resource {
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("findByIdOrName")
public void findByIdOrName (@FormParam("id") int id,
@FormParam("name") String name) {
System.out.println("=======");
System.out.println("id: " + id + " name:" + name);
}
}
This is how I did it, it's easy and it works. As for models, I honestly do not know.. I'm more of a C#.NET guy and I'm trying to learn Java.
Upvotes: 0
Reputation: 291
you are not sending xml to your controller. Check How to post XML to server thru HTML form?.
Fruitbean: add annotations to the getters or fields
Edit: you can test your webservice with rest-client
Edit2:
@Path("fruitstore")
public class FruitStore {
@POST
@Path("/checkobjectbyid")
@Consumes(MediaType.APPLICATION_XML)
public void loadObject(FruitBean bean) {
System.out.println("====================");
System.out.println("Fruit ID" + bean.getId() + " Name" + bean.getName());
}
@GET
@Path("/fruitbean")
@Produces(MediaType.APPLICATION_XML)
public Response getFruitBean(){
FruitBean fruitBean = new FruitBean();
fruitBean.setId(1L);
fruitBean.setName("name of fruitbean");
return Response.status(Response.Status.OK).entity(fruitBean).build();
}
}
Use lowercase characters for path. (url's are lowercase)
Use correct Consume and Produces annotations.
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
Part of web.xml
urls:
POST http://localhost:8080/PROJECTNAME/resources/fruitstore/checkobjectbyid
GET http://localhost:8080/PROJECTNAME/resources/fruitstore/fruitbean
Testing with rest-client
URL: http://localhost:8080/PROJECTNAME/resources/fruitstore/checkobjectbyid
METHOD: POST
CONTENT-TYPE: application/xml
CHARSET: UTF-8
BODY: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><fruitbean id="1" name="name of fruitbean"/>
Upvotes: 2
Reputation: 4693
First of all: in your index.htm
: method="POST"
and in the web-service code: @PUT
. Then this is no a form action - this is just a body of your request. Try using rest-cliet as Err suggested or a Chrome cREST Client.
Upvotes: 0