0

i try to convert a tutorial containing a very simple REST service (that should be deployed into karaf).

The blueprint definiton is

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xmlns:cxf="http://cxf.apache.org/blueprint/core"
xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0  http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://www.osgi.org/xmlns/blueprint-ext/v1.1.0 https://svn.apache.org/repos/asf/aries/tags/blueprint-0.3.1/blueprint-core/src/main/resources/org/apache/aries/blueprint/ext/blueprint-ext.xsd http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.1.0 http://aries.apache.org/schemas/blueprint-ext/blueprint-ext-1.1.xsd">

    <cxf:bus id="personRestBus">
    </cxf:bus>
    <bean id="personServiceImpl" class="net.lr.tutorial.karaf.cxf.personrest.impl.PersonServiceImpl"/>
    <jaxrs:server address="/person" id="personService">
        <jaxrs:serviceBeans>
            <ref component-id="personServiceImpl" />
        </jaxrs:serviceBeans>
        <jaxrs:features>
            <cxf:logging />
        </jaxrs:features>
    </jaxrs:server>
</blueprint>

The PersonService interface is super simple:

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface PersonService {
    @GET
    @Path("/")
    public Person[] getAll();

    @GET
    @Path("/{id}")
    public Person getPerson(@PathParam("id") String id);

    @PUT
    @Path("/{id}")
    public void updatePerson(@PathParam("id") String id, Person person);

    @POST
    @Path("/")
    public void addPerson(Person person);
}

The model looks like this:

package net.lr.tutorial.karaf.cxf.personrest.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {
    String id;
    String name;
    String url;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

}

And this is the imlementation: package net.lr.tutorial.karaf.cxf.personrest.impl;

import java.util.HashMap;
import java.util.Map;

import net.lr.tutorial.karaf.cxf.personrest.model.Person;
import net.lr.tutorial.karaf.cxf.personrest.model.PersonService;

public class PersonServiceImpl implements PersonService {
    Map<String, Person> personMap;

    public PersonServiceImpl() {
        personMap = new HashMap<String, Person>();
        Person person = createExamplePerson();
        personMap.put("1", person);
    }

    private Person createExamplePerson() {
        Person person = new Person();
        person.setId("1");
        person.setName("Ruediger");
        return person;
    }

    public Person[] getAll() {
        return personMap.values().toArray(new Person[]{});
    }

    public Person getPerson(String id) {
        return personMap.get(id);
    }

    public void updatePerson(String id, Person person) {
        person.setId(id);
        System.out.println("Update request received for " + person.getId() + " name:" + person.getName());
        personMap.put(id, person);
    }

    public void addPerson(Person person) {
        System.out.println("Add request received for " + person.getId() + " name:" + person.getName());
        personMap.put(person.getId(), person);
    }

}

The tests look like this: package net.lr.tutorial.karaf.cxf.personservice.impl;

import java.io.InputStream;

import javax.ws.rs.core.Response;

import net.lr.tutorial.karaf.cxf.personrest.impl.PersonServiceImpl;
import net.lr.tutorial.karaf.cxf.personrest.model.Person;
import net.lr.tutorial.karaf.cxf.personrest.model.PersonService;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class PersonServiceRestTest {

    private static final String PERSONSERVICE_TESTURL = "http://localhost:8282/person";
    private static Server server;

    @BeforeClass
    public static void startServer() {
        PersonService personService = new PersonServiceImpl();;
        JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
        factory.setAddress(PERSONSERVICE_TESTURL);
        factory.setServiceBean(personService);
        server = factory.create();
        server.start();
    }

    @Test
    public void testInterface() {
        PersonService personService = JAXRSClientFactory.create(PERSONSERVICE_TESTURL, PersonService.class);
        Person person = new Person();
        person.setId("1002");
        person.setName("Christian Schneider");
        personService.updatePerson("1002", person);

        Person person2 = personService.getPerson("1002");
        assertCorrectPerson(person2);
    }

    @Test
    public void testWebClient() {
        WebClient client = WebClient.create(PERSONSERVICE_TESTURL + "/1001");
        putPerson(client);
        Person person = client.get(Person.class);
        assertCorrectPerson(person);
    }

    private void putPerson(WebClient client) {
        InputStream is = this.getClass().getResourceAsStream("/person1.json");
        Response resp = client.put(is);
        System.out.println(resp);
    }

    @AfterClass
    public static void stopServer() {
        server.stop();
    }


    private void assertCorrectPerson(Person person) {
        Assert.assertNotNull(person);
        Assert.assertEquals("Christian Schneider", person.getName());
    }

}

The testWebClient test produces this WARNING, but this means i do not get a result:

2016-08-16 11:02:52,306 [tp1293680734-19] WARN WebApplicationExceptionMapper - javax.ws.rs.ClientErrorException: HTTP 415 Unsupported Media Type at org.apache.cxf.jaxrs.utils.SpecExceptions.toHttpException(SpecExceptions.java:117) at org.apache.cxf.jaxrs.utils.ExceptionUtils.toHttpException(ExceptionUtils.java:162) at org.apache.cxf.jaxrs.utils.JAXRSUtils.findTargetMethod(JAXRSUtils.java:530) at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:177) at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:253) at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:234) at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:70) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1129) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1065) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle(Server.java:499) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) at java.lang.Thread.run(Thread.java:745)

And so the test fails.

the testInterface test produces this error:

2016-08-16 11:20:34,232 [main ] WARN PhaseInterceptorChain - Interceptor for {http://model.personrest.cxf.karaf.tutorial.lr.net/}PersonService has thrown exception, unwinding now org.apache.cxf.interceptor.Fault: No message body writer has been found for class net.lr.tutorial.karaf.cxf.personrest.model.Person, ContentType: application/json at org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.doWriteBody(ClientProxyImpl.java:882) at org.apache.cxf.jaxrs.client.AbstractClient$AbstractBodyWriter.handleMessage(AbstractClient.java:1091) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:649) at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:747) at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:228) at com.sun.proxy.$Proxy19.updatePerson(Unknown Source) at net.lr.tutorial.karaf.cxf.personservice.impl.PersonServiceRestTest.testInterface(PersonServiceRestTest.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: javax.ws.rs.ProcessingException: No message body writer has been found for class net.lr.tutorial.karaf.cxf.personrest.model.Person, ContentType: application/json

I am able to deploy the service into karaf and interact. But the tests do not work out. I would like to avoid external frameworks like jackson, if possible.

Thank you!

Fluffy
  • 299
  • 1
  • 4
  • 21

1 Answers1

0

Your service is accepting any MediaType because you have not set a default value for @Consumes (see documentation). It is recommendable to set the content-type to accept

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface PersonService {

You need to set the content type in your client

client.type( MediaType.APPLICATION_JSON);

I am not sure, but I think you need to define the JSON provider in <jax-rs_server>

<bean id="jackson" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />

<jaxrs:server address="/person" id="personService">
    <jaxrs:serviceBeans>
        <ref component-id="personServiceImpl" />
    </jaxrs:serviceBeans>
    <jaxrs:features>
        <cxf:logging />
    </jaxrs:features>
   <jaxrs:providers>
        <ref bean="jackson" />
    </jaxrs:providers>
</jaxrs:server>
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • Thanks, i changed the service definition as you said. The thing is: I would like to deploy this service into karaf, so i have a blueprint xml. Do i need to use jackson? Or can i achieve the same result without? – Fluffy Aug 16 '16 at 09:13
  • CXF and JAXB needs an additional package to deal with JSON. http://stackoverflow.com/questions/38789307/how-to-serialize-jaxb-object-to-json-with-jaxb-reference-implementation/38831401#38831401. `Jackson` is a common dependency for `CXF,` and is possible it was included in your deployment (look for jackson*). If not, then you can find the maven configuration here http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-Jackson. Also is possible to use `Moxy` or `Jettison` – pedrofb Aug 16 '16 at 09:31
  • Your problem now in the deserialization of the bean `Person`. Your server does not know how to do it in JSON. See http://stackoverflow.com/questions/11773846/error-415-unsupported-media-type-post-not-reaching-rest-if-json-but-it-does-if – pedrofb Aug 16 '16 at 09:43
  • When i deploy the service to karaf, i can happily interact with it. I can post JSON files and create person objects, i can also get a list of PERSONs or retrieve a single one. So the serialization and deserialization does work. So there is a dependency in karaf that is fulfilled for JSON. but if i do a feature-list and grep for search termes, i get camel-jackson for jackson camel-restlet for rest, camel-xmljson for json, but no standalone jettison or jackson. How can this work? – Fluffy Aug 16 '16 at 09:59
  • However the JSON dependency is fulfilled in my karaf, i see i need a JSON Provider like Jackson. I added the maven dependency. Do i need to put the bean definition for jackson into a new web.xml file? I only have the blueprint xml file so far. – Fluffy Aug 16 '16 at 10:20
  • `camel-jackson` depends on `jackson`, and `camel-xmljson` is a converter XML<-->JSON. Maybe you have `jettison` deployed (old alternative). You can define the JSON provider in the same file – pedrofb Aug 16 '16 at 10:29
  • Thanks. But i thought blueprint would only be used when deploying to karaf. Is it also used with local tests? I now got my maven dependencies set up and the bean definition you suggested. However, the problem persists. – Fluffy Aug 16 '16 at 11:50
  • How are you publishing the CXF endpoint to execute your integration tests? You need a deployed `` to test the JSON provider. It will not work with unit testing. I am not a karaf user, but I think blueprint is used to deploy spring files, so any bean found should be injected. If the `provider` reference is not found then the JAX-RS server will not start. If the problem persists, maybe the jackson libraries are not really deployed. Additionally, are you using CXF 3.x? – pedrofb Aug 16 '16 at 12:06
  • Check also this post https://gautirao.wordpress.com/2016/06/09/configuring-jackson-json-provider-in-talend-esb-apache-karaf-apache-cxf/ May be you need to configure `skip.default.json.provider.registration` property in `` – pedrofb Aug 16 '16 at 12:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121066/discussion-between-fluffy-and-pedrofb). – Fluffy Aug 16 '16 at 13:52
  • Ok, I have answered there – pedrofb Aug 17 '16 at 07:35