415-UnSupported Media Type - rest

While performing testing RESTFUL Web Service using POSTMAN, I encountered the below error :
415 UnSupported Media Type
Currently in my code, I'm using MediaType.TEXT_PLAIN. This is due to one of the answer from page enter link description here telling that if you need to return integer, you need to use TEXT_PLAIN.
May I know is the data that I provide in the web service is compatible with TEXT_PLAIN or not.
#POST
#Path("/post")
#Produces(MediaType.TEXT_PLAIN)
public int adaptiveAuth( #FormDataParam("uuid") String uuID,
#FormDataParam("browserinfo") String browserInfo,
#FormDataParam("ipint") long ipInt,
#FormDataParam("lat") double latiTude,
#FormDataParam("longitude") double longiTude,
#FormDataParam("sessionid") String sessionID,
#FormDataParam("spid") String spID,
#FormDataParam("tr") int tR,
#FormDataParam("jsnum") int jsNum,
#FormDataParam("fingerprint") String fingerPrint,
#FormDataParam("methodset") MethodClass[][] methodSet) throws SQLException{
The way I tested in Postman are describe as below:

Solution.
1. Remove header value in SOAP UI.
2. I was unable to process an array in Jersey. Instead of process MethodClass [][] methodSet, I'm sending the value one by one.
3. I also change back from MediaType.MULTIPART_FORM_DATA to MediaType.PLAIN_TEXT
My code is working now.
Thanks for the help.

Related

HTTP Status 405 - Method Not Allowed using Rest webservice

I am getting correct result when invoking method #GET, but as soon as i use #PUT method using URI localhost:8080/MyProject/rest/calculator/23, it is giving error i.e. HTTP Status 405 - Method Not Allowed.
Code is below:
#Path("/calculator")
public class CalcyRest {
#GET
#Path("plain/{name}")
#Produces(MediaType.TEXT_PLAIN)
public String getplain(#PathParam("name") String name){
return "this is plain text ... Hello : "+name;
}
#GET
#Produces(MediaType.TEXT_HTML)
public String getplain(){
return "<html><head><title></title></head><body><h1>this is html</h1></body> </html>";
}
#PUT
#Path("{studentRollNo}")
#Produces(MediaType.TEXT_PLAIN)
public String updateCal(#PathParam("studentRollNo") String strn){
return "updated successfully!";
}
}
First of all, you didn't really ask a question. You are just describing your problem. Here on SO it is very important to know how to ask a question.
Secondly, it would have been a good idea to have posted the URL for the GET request, in order for us to notice some possible subtle mistakes.
Thirdly, please make sure your request URL is good. I was expecting to see something like localhost:8080/MyProject/calculator/23. Why did you put rest inside the URL?
Check your web server configuration. $1 says it is set to reject PUT requests.

REST API getById using Integer or String?

I'm designing a REST JAX-RS API with Jersey.
I want to know what is the best practice to get an object by ID.
Do I need to map the ID on an Integer or a String
Solution 1:
/books/{id}
getById(#PathParam("id") Long id)
Solution 2:
/books/{id}
getById(#PathParam("id") String id)
My intention was to used Long because it is directly mapped on my database model using a Long...
If you need a long, make the parameter a long.
If JAX-RS can't map a path parameter requested by the client to long, it will return a 404 Not Found HTTP status code which is a good thing. If you allow String, your code will have to do this check. Let JAX-RS do the checking.
#GET
#Path("/books/{id}")
public Response getBook(#PathParam("id") long id) {
return Response.ok("book " + id).build();
}
A request for /books/123 will return book 123. A request for /books/foo will fail with 404 Not Found.

How to sort dojox.grid.DataGrid with wink-based REST API?

I'm using the Dojo datagrid client side, it works well and according to documentation it generates the following GET request when clicking on the column header:
GET http://localhost:8080/books/rest/books?sort(+isbn)
Problem is that I can't interpret the query parameter "sort(+isbn)" on the server side using the Apache Wink framework, because there's no value set for it. E.g. I'd expect something like "sort=+isbn" instead.
Here's my server side code:
#Path("/books")
public class BookServiceImpl implements BookService {
...
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getBook(#QueryParam("sort") String sortBy) {
System.out.println("Received Queryparam for sort is " + sortBy);
return "";
}
}
Since "sort(+isbn)" has no value assigned to it, it appears to be an invalid query parameter. Not sure why Dojo datagrid uses this convention.
Would appreciate help as to how to work around this on the Java side, ideally using Wink or another mechanism to process GET requests.
Try to use #Context UriInfo to get the full uri info, call to UriInfo.getQueryParameters to get all query params. I believe sort(+isbn) will be there.

Url as path parameter in restful api causes bad request

We are developing a restful api using jersey (1.9.1) and tomcat 5.5.
A given resource is identified with a urn and we would like to address a specific instance of that resource. In order to achieve this, we used the following code:
#Path("/XXXs")
public interface XXXResource {
#GET
#Path("{id}")
#Produces({ MediaType.APPLICATION_JSON })
XXXInfo getXXX(#PathParam("id") String id);
}
The idea is to address this resource using the following url:
http://localhost:8080/restapi/XXXs/http%3A%2F%2Fns.something.com%2FXXX%2F2
The decoded path param value should be:
http://ns.something.com/XXX/2
However, when I make the request using the encoded url I get a bad request message from tomcat. So my questions are:
Is it correct to use a Urn as a path parameter?
Why is tomcat considering this request as a bad request?
Just in case, I changed the signature of the method so that the parameter is taken from the query string and it worked fine, but I want the parameter to be part of the path.
Thanks.
Ok, I solved it by adding the following line in catalina.properties:
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

Why won't my WCF Data Service accept my querystring?

I'm using jqGrid to display some data to the user. One of the features needed is for users to be able to search the grid. I'm using a WCF Data Service to get this data and return it.
When I first started jqGrid development I just needed pagination (no searching required) and was successfully able to use the following method:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public String GetStuff(int? page, int? rows)
{
// pagination going on in here
}
Then I needed the search so I added a param of type string like so:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public String GetStuff(int? page, int? rows, string search)
{
// more stuff going on in here
}
and then I get a 400 Bad Request error. I assume its because of the search param, I just don't understand why int works (and even bool worked) but string doesn't. Does it have anything to do with the fact that WCF Data Services are RESTful?
I of course double-checked the query string that jqGrid sends (via ajax) and it matches the param name. There are some extra query string params that jqGrid sends that are not used (ignoring them worked fine with my original pagination code)
String literals in the query URL must be quoted with single quotes. So the above service operation would be called like this:
/GetStuff?page=1&rows=10&search='John'
Does your query string look like that?