jax rs - Jersey 2.1 - Consuming collection of POJO as JSON string -
i have collection of entities (list) need convert to/from json.
the pojo:
public class task { private long id; private string title; private boolean done; (...) }
jersey produces following result
[{"id":1,"title":"t1","done":false},{"id":2,"title":"t2","done":false}]
when call method:
@get @override @produces("application/json") public list<task> findall() { (...) return tasks; }
so far good.
now, need consume similar json string. assumed following method trick:
@put @consumes("application/json") public void save(list<task> tasks) { (...) }
but instead error below:
severe: line 1:0 no viable alternative @ input '"[{\"id\":1,\"title\":\"t1\",\"done\":true},{\"id\":2,\"title\":\"t2\",\"done\":false}]"'
what doing wrong? possible?
jersey's dependencies:
<dependency> <groupid>javax.ws.rs</groupid> <artifactid>javax.ws.rs-api</artifactid> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet</artifactid> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-moxy</artifactid> <version>2.1</version> </dependency>
here web.xml configuration
<servlet> <servlet-name>jersey servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>my.rest.package</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
it seems you're sending corrupted json entity in message body, like:
post http://localhost:9998/test/list accept: application/json content-type: application/json "[{\"a\":\"a\",\"b\":1,\"c\":1}]"
i able same error entity (line 1:0 no viable alternative @ input '"[{\"a\":\"a\",\"b\":1,\"c\":1}]"'
).
make sure you're sending valid json rest service, i.e. registering logging filter in application:
new resourceconfig() .packages("my.rest.package") // register logging filter , print entity. .register(new loggingfilter(logger, true));
Comments
Post a Comment