简体   繁体   中英

Jersey client in applet doesn't parse JSON

I have a simple application which can be used either as applet and standalone app. It sends a request to REST service and tries to display the result which is a List<City> .

City class code is as follows:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class City implements Serializable {

    private Integer cityId;
    private String name;

    public Integer getCityId() {
        return cityId;
    }

    public void setCityId(Integer cityId) {
        this.cityId = cityId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

It uses jersey-bundle-1.17 to send request and get a response.

Code to send request and get response:

ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client c = Client.create(clientConfig);
List<City> result = c.resource(resource url here).get(new GenericType<java.util.List<City>>(){});

After building the app i have two jar files - MyJar.jar with my app and jersey-bundle-1.17.jar. Class-Path to jersey is specifiend in the Manifest of MyJar.jar.

When i run my jar file as a standalone app (java -jar MyJar.jar) everything is ok, request is sent and response is received and deserialised to the List<City> . When i run my jar file as applet (in html page or appletviewer) request is sent, response is received but it can't deserialise response to the List<City> saying

A message body reader for Java class java.util.List, and Java type java.util.List<City>, and MIME media type application/json was not found

I tried to add JacksonJsonProvider.class to the list of Jersey providers, tried to deserialise just to String, to City[] - it always says that appropriate message body reader is no found when running from applet. And everything works fine when runnig as a desktop application.

Applet definitely loads jersy-bundle jar as i see it in server access logs and request is sent, but it looks so that it is loaded not correctly (maybe it ignores manifest of the jersey-bundle jar while loading, i don't know). Problem occures only when code is being executed from applet.

Could anyone help me?

The problem was that Jersey read properties and used reflection and all these actions were prohibited for untrusted applet. It wasn't obvious as there were no errors in Java Console. I had to put the parsing code to the try/catch block, catch any Throwable and print it to the Console and only then i could see errors about security restrictions.

To solve the problem i had to self-sign my applet jar file, jersey jar file and ALL other libs which were used by Jersey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM