我正在创建一个REST服务,它接受输入作为PathParam并生成JSON响应。代码工作正常,我可以构建响应。在返回响应之后,REST正在抛出一个错误,例如“可能找不到写入器或DATASOURCEPROVIDER FOR(ResponseClass)和MediaType application / json”。感谢。
答案 0 :(得分:2)
这就是我让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>
答案 1 :(得分: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>
答案 2 :(得分:-1)
我自己发现WinkApplication没有正确加载。 web.xml中的restsdkservice配置不正确,因为 JacksonJaxbJsonProvider 未加载到Wink中。终于在2天后得到了它。
谢谢大家!!