1

i'm trying to transfer a list of objects from a server to a client using a web service. The data should be transfered in json format. I'm using jersey for the JAX-RS implementation and gson to deserialise the json string on the client side. I kept getting a parse error, even though i was using the recommended practice for list deserilization with gson (see below). I have found out that the json string which i am getting in the response is different than the one which i should receive in order for the gson deserilizer to work.

This is the reponse i'm getting from the web service:

{"activity":[{"activityName":"c","id":"3"},{"activityName":"c","id":"3"},{"activityName":"c","id":"3"}]}

But this is the correct response i should receive in order for the gson to deserialize the string to a List: [{"id":3,"activityName":"c"},{"id":3,"activityName":"c"},{"id":3,"activityName":"c"}]

You can see the added header/wrapper "activity" in the begining of the first string.

Here is the web service code:

@Path("ws")
public class IiwsServices {

@POST
@Path("/listActivities")
@Produces(MediaType.APPLICATION_JSON)
public List<Activity> listActivities() {

    List<Activity> list = new ArrayList<Activity>();

    Activity act = new Activity();
    act.setId(1);
    act.setActivityName("a");
    list.add(act);

    act.setId(2);
    act.setActivityName("b");
    list.add(act);

    act.setId(3);
    act.setActivityName("c");
    list.add(act);
    return list;
}

This is the client code:

public class Main {
public static void main(String[] args) {
    try {

        Client client = Client.create();

        WebResource webResource = client
           .resource("http://localhost:8080/iiws.ws.test/ws/listActivities");

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                   .post(ClientResponse.class);

        if (response.getStatus() != 200) {
           throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatus());
        }

        String output = response.getEntity(String.class);

        System.out.println(output);

        List<Activity> list = new ArrayList<Activity>();

        Activity act = new Activity();
        act.setId(1);
        act.setActivityName("a");
        list.add(act);

        act.setId(2);
        act.setActivityName("b");
        list.add(act);

        act.setId(3);
        act.setActivityName("c");
        list.add(act);

        Gson gson = new GsonBuilder().create();
        String jsonString = gson.toJson(list);

        System.out.println(jsonString);

        List<Activity> list = gson.fromJson(output, new   TypeToken<List<Activity>>(){}.getType()); // <-- Deserialize exception here

        for (int i = 0; i < list.size(); i++) {
            Activity x = list.get(i);
            System.out.println(x);
        }

      } catch (Exception e) {

        e.printStackTrace();

      }

}

****OK I believe I have found the answer****

OK finally found solution Here i found a similar question after googling for jersey and jackson : Jersey client can not deserializable json services

And this helps as well: JSON POJO support

Button line is that if not explicitely defined in the web.xml, jersey+jaxb is used instead of jersey+jackson


Error 403 as I try to open phpmyadmin. What do I do to correct it?

As I try open localhost/phpmyadmin/ I get the following error :

Access forbidden!

New XAMPP security concept:

Access to the requested directory is only available from the local network.

This setting can be configured in the file "httpd-xampp.conf".

If you think this is a server error, please contact the webmaster.

Error 403

Why is that ? I opened the httpd-xampp.conf file but I don't know what to do in that.

This is the complete httpd-xampp.conf file :

<IfDefine PHP4>
LoadModule php4_module        modules/libphp4.so
</IfDefine>
<IfDefine PHP5>
LoadModule php5_module        modules/libphp5.so
</IfDefine>
# Disabled in XAMPP 1.8.0-beta2 because of current incompatibilities with Apache 2.4
# LoadModule perl_module        modules/mod_perl.so


Alias /phpmyadmin "/opt/lampp/phpmyadmin"
Alias /phpsqliteadmin "/opt/lampp/phpsqliteadmin"

# since XAMPP 1.4.3
<Directory "/opt/lampp/phpmyadmin">
    AllowOverride AuthConfig Limit
    Order allow,deny
    Allow from all
</Directory>

<Directory "/opt/lampp/phpsqliteadmin">
    AllowOverride AuthConfig Limit
    Order allow,deny
    Allow from all
</Directory>

# since LAMPP 1.0RC1
AddType application/x-httpd-php .php .php3 .php4

XBitHack on

# since 0.9.8 we've mod_perl
<IfModule mod_perl.c>
    AddHandler perl-script .pl
    PerlHandler ModPerl::PerlRunPrefork
    PerlOptions +ParseHeaders
    PerlSendHeader On
</IfModule>

# demo for mod_perl responsehandler
#PerlModule Apache::CurrentTime
#<Location /time>
#      SetHandler modperl
#      PerlResponseHandler Apache::CurrentTime
#</Location>

# AcceptMutex sysvsem is default but on some systems we need this
# thanks to jeff ort for this hint
#AcceptMutex flock
#LockFile /opt/lampp/logs/accept.lock

# this makes mod_dbd happy - oswald, 02aug06
# mod_dbd doesn't work in Apache 2.2.3: getting always heaps of "glibc detected *** corrupted double-linked list" on shutdown - oswald, 10sep06
#DBDriver sqlite3

#
# New XAMPP security concept
#
<LocationMatch "^/(?i:(?:xampp|security|licenses|phpmyadmin|webalizer|server-status|server-info))">
    Order deny,allow
    Deny from all
    Allow from ::1 127.0.0.0/8 \
        fc00::/7 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \
        fe80::/10 169.254.0.0/16

    ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</LocationMatch>
4

1 に答える 1

0

Web サーバーが返す json 文字列から、アクティビティの配列を持つアクティビティという名前のオブジェクトが応答にあることは明らかです。

したがって、この構造を試して応答を読み取った場合。

    パブリッククラスアクティビティ{
        プライベート int id;
        プライベート文字列のアクティビティ名;
        public int getId() {
            ID を返します。
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getActivityName() {
            activityName を返します。
        }
        public void setActivityName(String activityName) {
            this.activityName = activityName;
        }
   }

    パブリッククラスのレスポンス{
        プライベート リスト アクティビティ。

        public List getActivity() {
            返品活動;
        }

        public void setActivity(リスト活動) {
            this.activity = アクティビティ;
        }

    }

gson ライブラリを使用して読み取る

    Gson gson = 新しい Gson();
    レスポンス response = gson.fromJson(json, Response.class);

于 2013-05-17T12:06:40.397 に答える