https://kotlinlang.org logo
Title
h

Hexa

02/13/2019, 3:46 PM
Please can someone with JAX-RS and Kotlin have a look at this question: https://stackoverflow.com/q/54674096/9506917
s

Shawn

02/13/2019, 3:49 PM
first thing that comes to mind, maybe try adding a leading slash to your
@Path
argument on the get endpoint?
also I’d annotate the class itself with `@Produces(MediaType.APPLICATION_JSON)`if all your endpoints return JSON, but that’s a side observation not related to your path problems
oh, wait, I misread what you wrote
your
POST
endpoint is likely being called but 500ing because you probably don’t have anything set up to deserialize the json blob to
String
h

Hexa

02/13/2019, 4:01 PM
okay
ohh
so i may have to use jacksonmapper??
s

Shawn

02/13/2019, 4:01 PM
or something similar, yes
what’s the stack trace on the server side look like?
h

Hexa

02/13/2019, 4:02 PM
there's no stack trace as I can't run it in debug mode
it just says
> Task :run 
Feb 13, 2019 4:02:25 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:8080]
Feb 13, 2019 4:02:25 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Press any key to shutdown
<==========---> 80% EXECUTING [27s]
> :run
s

Shawn

02/13/2019, 4:03 PM
Well, there should still be something in the logs, I think
also why can’t you run it in debug mode?
weird
h

Hexa

02/13/2019, 4:03 PM
when I call the GET
i get this
Feb 13, 2019 4:03:16 PM org.glassfish.jersey.logging.LoggingInterceptor log
INFO: 2 * Server has received a request on thread grizzly-http-server-1
2 > GET <http://localhost:8080/myresource/test123>
2 > accept: */*
2 > host: localhost:8080
2 > user-agent: curl/7.54.0

Feb 13, 2019 4:03:16 PM org.glassfish.jersey.logging.LoggingInterceptor log
INFO: 2 * Server responded with a response on thread grizzly-http-server-1
2 < 200
2 < Content-Type: application/json
hello
so it seems like POST does not even reach my server???
s

Shawn

02/13/2019, 4:04 PM
No, this seems like a failure of the logging configuration.
do you have logback or anything configured? or maybe you could run your application without using gradle?
It doesn’t seem useful to you to suppress stack traces
h

Hexa

02/13/2019, 4:08 PM
wait a sec
maybe i need to create a data class to represent this json?
{"message": "foo"}'
s

Shawn

02/13/2019, 4:08 PM
that is what most folks do, yes
h

Hexa

02/13/2019, 4:09 PM
would
Message(message: String)
do??
s

Shawn

02/13/2019, 4:09 PM
if you’re using Jackson, the kotlin mapper can automagically deserialize data classes for you
t

tddmonkey

02/13/2019, 4:09 PM
while you’re testing, just accept a
Map<Any, Any>
Jackson also has a
KotlinModule
you will need to register
It’s in
jackson-module-kotlin
h

Hexa

02/13/2019, 4:10 PM
did you mean change my function to
fun createMessage(testPost: Map<Any, Any>)
?
that didnt worked
just tried it
let me try jacksonmapper
j

Joe

02/13/2019, 4:14 PM
also you seem to be accepting/producing raw strings right now, does it work with MediaType.PLAIN_TEXT? -- jersey doesn't particularly support raw strings as json, so either label strings as plain text or return json-able entity classes is best
h

Hexa

02/13/2019, 4:24 PM
not working still, even with Plain text
I was just following this exact same repo and changed just this class: https://github.com/baens/blog-step-by-step-jaxrs/blob/master/src/jaxrs/resources/HelloWorld.kt
problem still the same even after i simplified my POST function to this @Joe
@POST
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    fun createMessage(message: String): Response {
        return Response.status(200).entity("test").build()
    }
it just seems like its not reaching my server at all
okay I got a 200 after sending a POST with no data like this
curl -v -X POST \
  <http://localhost:8080/messages> \
  -H 'Content-Type: application/json'
decided to moved to spring in the end. much easier
s

Shawn

02/13/2019, 8:10 PM
shrug
fwiw I’ve been pretty happy using Dropwizard and Kotlin together
👆 1
j

Joe

02/13/2019, 9:11 PM
yeah, i keep meaning to look @ ktor, but having much success w/ dropwizard already
t

tddmonkey

02/13/2019, 9:19 PM
Yeah, we use DW + kotlin extensively