0

I have some java classes which were generated using axis wsdl2java. I want to use those classes to be returned in Json format, but i keep getting various error, such as cannot convert Incident Object to HttpResponse try to make it writable.

When tried using Writable it asks me to check if it have apply method.

Is there any swift way i can use java classes as is, without going through hassle of making it case classes.

chetank
  • 392
  • 3
  • 17
  • Is your problem that you need an easy way to generate Play json `Writes`/`Format` for a Java class? – Simon Feb 22 '18 at 17:15
  • Yes basically I was trying to respond it in Json format with chunked data. So that client doesn’t have to wait h till all the data has been passed on to him as a response. – chetank Feb 23 '18 at 18:23
  • I believe that neither play-json nor circe will work with Java classes, unless you write a custom `Writes` or `Encoder`. Perhaps this could be useful: https://stackoverflow.com/questions/6388589/scala-equivalent-to-wsdl2java – Simon Feb 24 '18 at 23:37

1 Answers1

1

You try to render a class directly with Ok(MyClass()) and this is not possible. You have to render it as Json or String or any Writable. Json is probably the best choice. You should use a library like play-json, circe or one of the several others and render you response with Ok(Json.toJson(Myclass())) (for play-json) or equivalent.

Simon
  • 6,025
  • 7
  • 46
  • 98
  • trying to do something like this . `val source = Source.apply[Incident](incidentsList.asScala.toList) Ok.chunked(source)`. Will it work with the above suggestion you mentioned. Forgive my lack of knowledge. just a new bie – chetank Feb 21 '18 at 15:34
  • First you will have to add a json library to your dependencies in your build.sbt file. After you will have to write a json writer for the type of source. Do you really want to chunk your response? – Simon Feb 21 '18 at 15:39
  • i tried without the chunked format as well. But it is bit too much to write as json writer for that class, since it contains around 60+ attributes to that class. Dont we have anything like Gson library for scala. – chetank Feb 21 '18 at 16:04
  • If you use circe you have nothing to write and just to add one import. If you use play-json you only need to write implicit val myImplicitRead: OFormat[MyClass] = Json.format[MyClass] But with play-json I think that you will have a problem cause your class has more than 22 attributes (known problem). – Simon Feb 21 '18 at 16:21