Composite service + RESTEasy binding + JSON messages
greg.moulds Sep 15, 2014 4:37 PMHi folks,
I've been playing around with SwitchYard 2.0.0.Alpha2 and had some moderate success however I've run across an issue that I could use some guidance with.
I'm trying to create a composite service with a RESTEasy binding that speaks JSON rather than XML. I've based my service on the OrderService of the rest-binding quickstart application.
My very simple service looks like so:
@Service(Greeting.class) public class GreetingBean implements Greeting { public String helloWorld() { return "Hello, world."; } public String generate(UserBio bio) { String msg = String.format("Greetings %s %s!", bio.getFirstName(), bio.getLastName()); return msg; } }
My model class looks like so:
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class UserBio { private String firstName; private String lastName; public UserBio() { } public UserBio(String firstName, String lastName) { this.setFirstName(firstName); this.setLastName(lastName); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
My RESTEasy resource mapping looks like so:
import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; @Path("/greeting") public interface GreetingResource { @GET @Path("/") public String helloWorld(); @PUT @Path("/generate") @Consumes({ "application/json" }) public String generate(UserBio bio); }
Note that the generate() method has been annotated such that it is expecting to consume JSON rather than XML content.
It's my understanding that RESTEasy JAX-RS has built-in support for JSON messages and that that is all I have to do get my service to speak JSON.
That's probably wrong though because when I run a unit test like this:
@Test public void greetingServiceRESTEndpoint() throws Exception { // Hello World String response = http.sendString(BASE_URL + "/greeting", "", HTTPMixIn.HTTP_GET); Assert.assertEquals(HELLO_WORLD, response); // Generate a greeting http.setContentType("application/json"); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(userBio1); response = http.sendString(BASE_URL + "/greeting/generate", jsonString, HTTPMixIn.HTTP_PUT); Assert.assertEquals(SUCCESS, response); }
The helloWorld response is successful however the 2nd http send fails with the following response from the server:
HTTP/1.1 400 Could not find message body reader for type: class com.acme.bhn_test_service.UserBio of content type: application/json;charset="UTF-8" [Connection: keep-alive, Content-Length: 0]
Any help from the hive mind is greatly appreciated.
Thx.
Edit: -- Forgot to mention that I will be deploying this on wildfly eventually however the unit test uses Netty as per the rest-binding sample... in case that's relevant...