0

我正在创建一个 REST 服务,它接受作为 PathParam 的输入并生成 JSON 响应。代码运行良好,我可以构建响应。返回响应后,REST 会抛出类似“无法找到 WRITER OR DATASOURCEPROVIDER FOR (ResponseClass) and MediaType application/json”的错误。谢谢。

4

3 回答 3

3

这就是我如何让 jackson 2.1.3 与 Wink 作为 JSON 提供程序一起工作。

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

//see  http://www.ibm.com/developerworks/web/library/wa-aj-jackson/
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**
 * @see https://cwiki.apache.org/WINK/jax-rs-getting-started.html
 */
public class JaxrsApp extends Application {

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(YourJaxRSAnnotated.class);
    //...
    return classes;

}
 @Override
  public Set<Object> getSingletons() {
    Set<Object> s = new HashSet<Object>();

    // See http://wiki.fasterxml.com/JacksonJAXBAnnotations for more information
    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector primary = new JaxbAnnotationIntrospector( TypeFactory.defaultInstance() );
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector pair = AnnotationIntrospector.pair(primary, secondary);
    mapper.getDeserializationConfig().with(pair);
    mapper.getSerializationConfig().with(pair);

    // Set up the provider
    JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider();
    jaxbProvider.setMapper(mapper);

    s.add(jaxbProvider);
    return s;
  }
}

然后,在 web.xml

<servlet>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
    <!--  this param tells the RestServlet which custom Application sub-class
    will return a list of our classes that have JAX-RS annotations -->
    <init-param>
              <param-name>javax.ws.rs.Application</param-name>
              <param-value>com.example.JaxrsApp</param-value>
            </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
于 2013-01-23T22:55:53.583 回答
0

我想你用的是泽西岛。

首先,检查依赖关系:http: //jersey.java.net/nonav/documentation/latest/chapter_deps.html#d4e1716

这是我的 pom.xml:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.15</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.15</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.15</version>
    </dependency>
于 2012-12-27T22:33:51.413 回答
-1


I myself found that the WinkApplication is not loaded properly. The restsdkservice configuration in web.xml was incorrect and because of that JacksonJaxbJsonProvider was not loaded into the Wink. Finally got it after 2 days.
Thanks everyone!!

于 2012-12-31T09:15:00.527 回答