I created a simple GrizzlyHttpServerFactory.createHttpServer service and trying to issue a @GET operation, where the client would pass in arguments, however I am receiving an error:
I perused several examples and everything looks fine. Everything works, so long as I do not try and pass parameters. Here is the code that I have so far.
in main()
final ResourceConfig resourceConfig = new ResourceConfig(TestServerResource.class);
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:8081/base/"), resourceConfig);
In Resource Class:
@Path("TestServer")
public class TestServerResource
{
public static final String CLICHED_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<note>\r\n<to>Tove</to>\r\n<from>Jani</from>\r\n<heading>Reminder</heading>\r\n<body>Don't forget me this weekend!</body>\r\n</note>\r\n\r\n";
public static final String DO_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DoTask>\r\n\t<Response>%s</Response>\r\n</DoTask>\r\n\r\n";
@GET
@Path("getHello")
@Produces(MediaType.APPLICATION_XML)
public String getHello()
{
return CLICHED_MESSAGE;
}
@GET
@Path("testGet3")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_XML)
public String testGet3(MultivaluedMap<String, String> formParams)
{
// Get the parameters.
String argTitle = formParams.getFirst("title");
String argAuthor = formParams.getFirst("author");
// Create the XML response.
String xmlResponse = String.format(DO_MESSAGE, String.format("Book Title %s with author %s", argTitle, argAuthor));
return xmlResponse;
}
}
After service running on my CentOS 7 box. Here is terminal command.
Works (no parameters):
[test@Turbo Downloads]$ curl -H "Accept: application/xml" "http://localhost:8081/base/TestServer/getHello"
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
With parameters does NOT work:
[test@Turbo Downloads]$ curl -H "Accept: application/xml" "http://localhost:8081/base/TestServer/testGet3?title=smurfs&author=Gargamel"
<html><head><title>Grizzly 2.3.8</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.3.8</div></body></html>[test@Turbo Downloads]$
I did wrap the url in quotes, when passing arguments, so that is not the problem. Thoughts?
Here are just a few of the resources that I perused:
Specifying a header to curl with the -H does not matter seem to matter in this particular example, when not using arguments, as I tried both ways and I get the proper XML back. I do not have to specify the -X option, as GET is the default operation, which is what I am specifying. Single and double quotes yield the same result.
I tried to implement GrizzlyWebContainerFactory, as mentioned in 24518607, but I could not get ant to locate that jar, even though Eclipse was happy enough. I did not want to get side tracked, so I stayed focused.
Thoughts?