unmanaged extension : to process List of json strings

105 views
Skip to first unread message

Shireesh

unread,
Oct 1, 2012, 11:05:24 AM10/1/12
to ne...@googlegroups.com
Hi,

i wrote an unmanaged plugin which accepts List<Strings> as input.


@javax.ws.rs.Path("/process-json")
public class Process{
@POST
public final Response processJson(final List<String> listOfJson){
for(String json : listOfJson)
{
 processJsonString(json);
}
}
}

and iam invoking this from RESTClient

POST http://localhost:7474/ext/unmanaged/process-json
Body : [{node1:[{"key1":1}]}]
Content-Type : "application/json"

But iam getting the following exception

HTTP/1.1 415 Unsupported Media Type

Tero Paananen

unread,
Oct 1, 2012, 11:31:46 AM10/1/12
to ne...@googlegroups.com
> @javax.ws.rs.Path("/process-json")
> public class Process{
> @POST
> public final Response processJson(final List<String> listOfJson){
> for(String json : listOfJson)
> {
> processJsonString(json);
> }
> }
> }
>

Annotate processJson with:

@Consumes(MediaType.APPLICATION_JSON)

import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType; // "application/json"

-TPP

Shireesh

unread,
Oct 2, 2012, 12:07:05 AM10/2/12
to ne...@googlegroups.com

Thanks for the reply Tero,

I already tried with @Consumes annotation, but got the same exception.

And as per the documentation,
If we do not mention any MIME Type
"By default, a resource class can respond to and produce all MIME media types of representations specified in the HTTP request and response headers."

So does it mean it will very well accept Json ?

Any inputs are most welcomed.

Thanks,
Shireesh.

Peter Neubauer

unread,
Oct 2, 2012, 5:08:05 PM10/2/12
to ne...@googlegroups.com
Shireesh,
just tried this:



@Path("/helloworld")
public class HelloWorldResource {
private final GraphDatabaseService database;

public HelloWorldResource(@Context GraphDatabaseService database) {
this.database = database;
}

@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
@Path("/{nodeId}")
public Response hello(@PathParam("nodeId") long nodeId) {
// Do stuff with the database
return Response.status(Status.OK)
.entity(("Hello World, nodeId=" + nodeId).getBytes()).build();
}
}



And the server complains rightfully for application/json, and not for
text/plain:

➜ server-examples git:(working) ✗ curl -H
"Content-Type:application/json" -v
http://localhost:7474/test/helloworld/1
* About to connect() to localhost port 7474 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 7474 (#0)
> GET /test/helloworld/1 HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:7474
> Accept: */*
> Content-Type:application/json
>
< HTTP/1.1 415 Unsupported Media Type
< Access-Control-Allow-Origin: *
< Content-Type: text/html; charset=iso-8859-1
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Length: 1408
< Server: Jetty(6.1.25)
<
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 415 Unsupported Media Type</title>
</head>
<body><h2>HTTP ERROR 415</h2>
<p>Problem accessing /test/helloworld/1. Reason:
<pre> Unsupported Media Type</pre></p><hr /><i><small>Powered by
Jetty://</small></i><br/>



➜ server-examples git:(working) ✗ curl -H "Content-Type:text/plain"
-v http://localhost:7474/test/helloworld/1
* About to connect() to localhost port 7474 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 7474 (#0)
> GET /test/helloworld/1 HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:7474
> Accept: */*
> Content-Type:text/plain
>
< HTTP/1.1 200 OK
< Content-Length: 21
< Content-Type: text/plain
< Access-Control-Allow-Origin: *
< Server: Jetty(6.1.25)
<
* Connection #0 to host localhost left intact
Hello World, nodeId=1* Closing connection #0

------------

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Wanna learn something new? Come to http://graphconnect.com
> --
>
>

Shireesh

unread,
Oct 4, 2012, 3:51:46 AM10/4/12
to ne...@googlegroups.com

Thankz Peter.

Peter Neubauer

unread,
Oct 4, 2012, 7:21:00 PM10/4/12
to ne...@googlegroups.com
Welcome!

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html
> --
>
>
Reply all
Reply to author
Forward
0 new messages