当我声明RestTemplate类时为什么不加载类?

时间:2019-01-30 19:06:34

标签: java spring

在绑定到jsf页面的bean类中,单击按钮时我进行了REST调用,但是RestTempleate不会加载类 com.fasterxml.jackson.databind.ObjectMapper和com.fasterxml.jackson.core.JsonGenerator 因此我无法映射JSON响应。

JSF代码:

<p:commandButton value="Add"  action="#{myBean.addPerson}" styleClass="ui-priority-primary" />

Java类“ myBean”:

public void addPerson(){
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Person> response = restTemplate.exchange(myURL, HttpMethod.GET, null, Person.class);}

在没有加载类的RestTemplate类中,jackson2Present为false:

static {
    ClassLoader classLoader = RestTemplate.class.getClassLoader();
    romePresent = ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", classLoader);
    jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", classLoader);
    jackson2Present =
            ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
                    ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
    jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
    gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);

}

public RestTemplate() {
    this.messageConverters.add(new ByteArrayHttpMessageConverter());
    this.messageConverters.add(new StringHttpMessageConverter());
    this.messageConverters.add(new ResourceHttpMessageConverter(false));
    try {
        this.messageConverters.add(new SourceHttpMessageConverter<>());
    }
    catch (Error err) {
        // Ignore when no TransformerFactory implementation is available
    }
    this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

    if (romePresent) {
        this.messageConverters.add(new AtomFeedHttpMessageConverter());
        this.messageConverters.add(new RssChannelHttpMessageConverter());
    }
    if (jackson2XmlPresent) {
        this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
    }
    else if (jaxb2Present) {
        this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
    } if (gsonPresent) {
        this.messageConverters.add(new GsonHttpMessageConverter());
    }

    this.uriTemplateHandler = initUriTemplateHandler();
}

0 个答案:

没有答案