Could not find acceptable representation (已解决)

解决方案1:

@ResponseBody 方法的返回类型由JSONObject改为 Object, return时直接返回Object;

修改springmvc-servlet.xml, 增加 messageConverters 

1

2

3

4

5

6

7

8

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

"messageConverters">

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

需要jacksonjar包支持: jackson-core-lgpl-1.2.1.jar和 jackson-core-lgpl-1.2.1.jar。  

解决方案2:

@ResponseBody 方法的返回类型由JSONObject改为 String, return时直接返回JSONObject.fromObject(object).toString();

此时如有中文会出现乱码,解决方法如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

       

       

       

       "messageConverters"

        

           

               class="org.springframework.http.converter.StringHttpMessageConverter"

               "supportedMediaTypes"

                    

                       class="org.springframework.http.MediaType"

                           "0" value="text" /> 

                           "1" value="plain" /> 

                           "2" value="UTF-8" /> 

                        

                    

                

            

        

    

   

  

 

解决方案3:

返回类型为JSONObject, 编写一个自己converter.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

package javacommon.util;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.nio.charset.Charset;

 

import org.springframework.http.HttpInputMessage;

import org.springframework.http.HttpOutputMessage;

import org.springframework.http.MediaType;

import org.springframework.http.converter.AbstractHttpMessageConverter;

import org.springframework.http.converter.HttpMessageNotReadableException;

import org.springframework.http.converter.HttpMessageNotWritableException;

 

public class JaksonConverter extends AbstractHttpMessageConverter {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

     

    public JaksonConverter() {

        super(new MediaType("application""json", DEFAULT_CHARSET));

    }

     

    @Override

    protected boolean supports(Class clazz) {

        // TODO Auto-generated method stub

        return false;

    }

 

    @Override

    protected Object readInternal(Classextends Object> clazz,

            HttpInputMessage inputMessage) throws IOException,

            HttpMessageNotReadableException {

        logger.info(clazz.getSimpleName());

        InputStream inputStream=inputMessage.getBody();

        StringBuilder stringBuilder = new StringBuilder();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line = null;

        while((line = bufferedReader.readLine()) != null){

            stringBuilder.append(line);

        }

        logger.info(stringBuilder.toString());

        return stringBuilder;

    }

 

    @Override

    protected void writeInternal(Object t, HttpOutputMessage outputMessage)

            throws IOException, HttpMessageNotWritableException {

        logger.info(t.getClass().getSimpleName());

         

        // HashMap

        // JSONObject

        logger.info(t.toString());

        OutputStream os=outputMessage.getBody();

          

        os.write(t.toString().getBytes("utf-8"));

        os.flush();

         

    }

}

VB恩文转自:https://www.cnblogs.com/leegtang/articles/4463178.html

 

你可能感兴趣的:(Could not find acceptable representation (已解决))