Spring中的Jersey应用程序的自定义消息正文编写器

时间:2017-06-28 08:36:29

标签: java rest spring-mvc jersey

我有一个使用Spring MVC和Jersey

的Rest Api
@GET
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public List<AccountEntity> hello() {
    return repository.findAll();

}

此方法失败,始终出现以下错误

SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<net.viralpatel.spring.controller.AccountEntity>, and MIME media type application/json was not found.

My Gradle Dependencies

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version: '4.3.9.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.4.RELEASE'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.10.Final'
    compile "javax.servlet:javax.servlet-api:3.0.1"
    compile "javax.servlet:jstl:1.2"
    compile 'org.glassfish.grizzly:grizzly-http-servlet-server:2.3.11@jar'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
    compile group: 'com.sun.jersey.contribs', name: 'jersey-spring', version: '1.19.4'

}

我尝试添加

compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.26-b06'

然后错误显示为javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;

My Project Structure

更新添加杰克逊依赖

caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<net.viralpatel.spring.controller.AccountEntity>, and MIME media type application/json was not found.

1 个答案:

答案 0 :(得分:1)

您必须添加 "query": { "regexp" : { "description" : ".*plan is.*" } } 依赖项,而不是jersey-jsonmoxy

jackson

此依赖项中已包含compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.4' 及其他必需依赖项。