找不到针对媒体type = text / plain,type = class TestRest.Track,genericType = class TestRest.Track

时间:2019-11-11 10:22:49

标签: java spring jar jackson jersey

请求JSON格式并成功返回字符串时,出现以下错误。

  

找不到针对媒体type = text / plain,type = class TestRest.Track,genericType = class TestRest.Track的MessageBodyWriter

我试图放下杰克逊罐子,但没有解决问题。

我正在使用以下jersey和jackson版本并得到错误消息。

1. javax.ws.rs-api-2.0.1.jar
2. aopalliance-repackaged-2.5.0-b32.jar
3. hk2-api-2.5.0-b32.jar
4. hk2-locator-2.5.0-b32.jar
5. hk2-utils-2.5.0-b32.jar
6. javassist-3.20.0-GA.jar
7. javax.annotation-api-1.2.jar
8. javax.inject-2.5.0-b32.jar
9. javax.servlet-api-3.0.1.jar
10. jaxb-api-2.2.7.jar
11. jersey-guava-2.25.1.jar
12. org.osgi.core-4.2.0.jar
13. osgi-resource-locator-1.0.1.jar
14. persistence-api-1.0.jar
15. validation-api-1.1.0.Final.jar
16. jersey-client.jar
17. jersey-common.jar
18. jersey-container-servlet.jar
19. jersey-container-servlet-core.jar
20. jersey-media-jaxb.jar
21. jersey-server.jar
22. jackson-annotations-2.9.0.jar
23. jackson-core-2.9.9.jar
24. jackson-databind-2.9.9.1.jar

我试图降低杰克逊版本,但问题仍然存在。 下面是我的web.XML配置文件。

WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>PSG</display-name>
<welcome-file-list>
  <welcome-file>login</welcome-file>
 </welcome-file-list>

 <servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
        </init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

<listener>
    <listener-class>
      org.springframework.security.web.session.HttpSessionEventPublisher
    </listener-class>
</listener>

 <context-param>    
  <param-name>contextConfigLocation</param-name>    
  <param-value>/WEB-INF/security-config.xml 
    /WEB-INF/spring-database.xml
  </param-value>    
 </context-param> 
<!-- Logging listener -->
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
    <!-- You can also define yourpath with a environment variable and use it like file:${env_variable_path}/log4j.properties  -->
  </context-param>
<filter>    
  <filter-name>springSecurityFilterChain</filter-name>    
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    
 </filter>    

 <filter-mapping>    
  <filter-name>springSecurityFilterChain</filter-name>    
  <url-pattern>/*</url-pattern>    

 </filter-mapping> 

<session-config>
<session-timeout>10</session-timeout>
</session-config>

      <servlet>

        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationcontext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>

    </servlet> 
   <servlet>
<servlet-name>JAVA API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>TestRest</param-value>
</init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- 
   <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>false</param-value>
    </context-param>
     -->
</web-app>

下面是我的java类

TACK.JAVA
package TestRest;

import org.jboss.logging.Logger;



public class Track {
    private static final Logger logger = Logger.getLogger(Track.class.getName());
    String title;
    String singer;

    public Track(){}

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    @Override
    public String toString() {
        String Concatst= "Track [title=" + title + ", singer=" + singer + "]";
        logger.info("------------"+Concatst);
        return Concatst;
    }

}

我打电话给我的JAVASERVICE班> JAVA班。

JSONSERVICE.JAVA
package TestRest;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.logging.Logger;

import TestRest.Track;

@Path("/json/metallica")
public class JSONService {
    private static final Logger logger = Logger.getLogger(Track.class.getName());
    @GET
    @Path("/get")
    @Produces(MediaType.TEXT_PLAIN)
    public Track getTrackInJSON() {

        Track track = new Track();
        track.setTitle("Enter Sandman");
        track.setSinger("Metallica");
        logger.info(track);
        return track;

    }

    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createTrackInJSON(Track track) {

        String result = "Track saved : " + track;
        return Response.status(201).entity(result).build();

    }
    }

1 个答案:

答案 0 :(得分:0)

问题是您正在指示Jersey(通过@Produces(MediaType.TEXT_PLAIN)注释)将响应序列化为text/plain MIME类型,但是它不知道如何执行。您显然希望响应使用JSON。

只需将@Produces(MediaType.TEXT_PLAIN)上的@Produces(MediaType.APPLICATION_JSON)更改为getTrackInJSON()