Spring 3.0 making JSON response using jackson message converter

后端 未结 8 1952
迷失自我
迷失自我 2020-12-01 05:20

i configure my messageconverter as Jackson\'s then

class Foo{int x; int y}

and in controller

@ResponseBody
public Foo metho         


        
相关标签:
8条回答
  • 2020-12-01 06:04

    This worked for me:

    @RequestMapping(value = "{p_LocationId}.json", method = RequestMethod.GET)
    protected void getLocationAsJson(@PathVariable("p_LocationId") Integer p_LocationId,
         @RequestParam("cid") Integer p_CustomerId, HttpServletResponse response) {
            MappingJacksonHttpMessageConverter jsonConverter = 
                    new MappingJacksonHttpMessageConverter();
            Location requestedLocation = new Location(p_LocationId);
            MediaType jsonMimeType = MediaType.APPLICATION_JSON;
            if (jsonConverter.canWrite(requestedLocation.getClass(), jsonMimeType)) {
            try {
                jsonConverter.write(requestedLocation, jsonMimeType,
                                       new ServletServerHttpResponse(response));
                } catch (IOException m_Ioe) {
                    // TODO: announce this exception somehow
                } catch (HttpMessageNotWritableException p_Nwe) {
                    // TODO: announce this exception somehow
                }
            }
    }
    

    Note that the method doesn't return anything: MappingJacksonHttpMessageConverter#write() does the magic.

    0 讨论(0)
  • 2020-12-01 06:06

    This is just a guess, but by default Jackson only auto-detects public fields (and public getters; but all setters regardless of visibility). It is possible to configure this (with version 1.5) to also auto-detect private fields if that is desired (see here for details).

    0 讨论(0)
  • 2020-12-01 06:08

    I found that I need jackson-core-asl.jar too, not only jackson-mapper-asl.jar

    0 讨论(0)
  • 2020-12-01 06:09

    You need the following:

    1. Set annotation-driven programming model: put <mvc:annotation-driven /> in spring.xml
    2. Place jaskson jar (Maven artifactId is org.codehaus.jackson:jackson-mapper-asl) in classpath.
    3. Use as the following:

      @RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
      public @ResponseBody Foo method(@Valid Request request, BindingResult result){
      return new Foo(3,4)
      }
      

    This works for me.

    Please note, that

    1. @ResponseBody is applied to return type, not to the method definition.
    2. You need @RequestMapping annotation, so that Spring will detect it.
    0 讨论(0)
  • 2020-12-01 06:11

    The MessageConverter interface http://static.springsource.org/spring/docs/3.0.x/javadoc-api/ defines a getSupportedMediaTypes() method, which in case of the MappingJacksonMessageCoverter returns application/json

    public MappingJacksonHttpMessageConverter() {
        super(new MediaType("application", "json", DEFAULT_CHARSET));
    }
    

    I assume a Accept: application/json request header is missing.

    0 讨论(0)
  • 2020-12-01 06:15

    A HTTP 404 error just means that the resource cannot be found. That can have 2 causes:

    1. Request URL is wrong (client side error or wrong URL in given link/button).
    2. Resource is not there where you expect it is (server side error).

    To fix 1, ensure you're using or providing the correct request URL (casesensitive!). To fix 2, check the server startup logs for any startup errors and fix them accordingly.

    This all goes beyond the as far posted code and information.

    0 讨论(0)
提交回复
热议问题