0

I can't figure out why my GET endpoint gets called but my POST endpoint is not working. When I call curl -v -X GET http://localhost:8080/myresource/test123 it succesfully returns hello

But when I call

curl -v -X POST \
  http://localhost:8080/myresource \
  -H 'Content-Type: application/json' \
  -d '{"test": "testvalue"}'

I keep getting this response:

* Connected to localhost (::1) port 8080 (#0)

> POST /myresource HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 21
> 
* upload completely sent off: 21 out of 21 bytes
< HTTP/1.1 500 Request failed.
< Content-Type: text/html;charset=ISO-8859-1
< Connection: close
< Content-Length: 1031
< 
* Closing connection 0
<html><head><title>Grizzly 2.4.0</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Request failed.</div><div class="body">Request failed.</div><div class="footer">Grizzly 2.4.0</div></body></html>%

Here is my code

import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response


@Path("myresource")
class HelloWorldResource {


    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    fun createMessage(testPost: String): Response {
        return Response.status(200).entity("helllo post").build()
    }


    @GET
    @Path("{testGet}")
    @Produces(MediaType.APPLICATION_JSON)
    fun getMessage(@PathParam("testGet") testGet: String): Response {
        return Response.status(200).entity("hello").build()
    }
}



Makoto
  • 104,088
  • 27
  • 192
  • 230
Jay F.
  • 51
  • 5
  • 5xx errors with JAX-RS represent some kind of exception being thrown. Do you have that exception handy? – Makoto Feb 13 '19 at 16:00
  • It can possible be due to `createMessage` taking `String` but your POST body contains JSON that might likely be marshalled into POJO with one `test` field. – tsolakp Feb 13 '19 at 16:03
  • Register [this DebugExceptionMapper](https://stackoverflow.com/a/31289875/2587435) with your application. Most likely you will see the exception that is being swallowed by the container. If after seeing the exception, you can't figure out how to fix it, post the exception/stacktrace here. – Paul Samsotha Feb 13 '19 at 21:27

1 Answers1

0

Without seeing the actual underlying exception, it's hard to say for sure, but likely you're running into similar issues as Jersey: Return a list of strings with mismatches between a String type and a MediaType.APPLICATION_JSON produces/consumes declarations. If you're dealing in raw strings, I'd suggest using MediaType.PLAIN_TEXT, or having your post body and return value be an entity that can be represented as a non-raw-string json object (ie, something enclosed in {}), and making sure a jackson provider is registered with jersey.

Joe
  • 607
  • 6
  • 13