1

そのため、タイトルにエラーが発生し、次のコードを使用しています。

..。

RestTemplate restTemplate = new RestTemplate();

restTemplate.getMessageConverters().add(
        new MappingJacksonHttpMessageConverter());

String url = "w/ever";
List<Teacher> teachers = (List<Teacher>) restTemplate.getForObject(
        url, List.class);

..。

私の先生のクラスは次のようになります。

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Teacher generated by hbm2java
 */
public class Teacher implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer id;
    private ClassGroup classGroup;
    // private Users users;
    private String lastName;
    private boolean admin;
    private String telephoneNo;
    private String address;
    private String email;
    private String firstName;
    private Set<SubjectTeacherForClass> subjectteacherforclasses = new HashSet<SubjectTeacherForClass>(
            0);

    public Teacher() {
    }

    public Teacher(String lastName, boolean admin, String email,
            String firstName) {
        // this.users = users;
        this.lastName = lastName;
        this.admin = admin;
        this.email = email;
        this.firstName = firstName;
    }

    public Teacher(ClassGroup classGroup, String lastName, boolean admin,
            String telephoneNo, String address, String email, String firstName,
            Set<SubjectTeacherForClass> subjectteacherforclasses) {
        this.classGroup = classGroup;
        // this.users = users;
        this.lastName = lastName;
        this.admin = admin;
        this.telephoneNo = telephoneNo;
        this.address = address;
        this.email = email;
        this.firstName = firstName;
        this.subjectteacherforclasses = subjectteacherforclasses;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public ClassGroup getClassgroup() {
        return this.classGroup;
    }

    public void setClassgroup(ClassGroup classGroup) {
        this.classGroup = classGroup;
    }

    /*
     * @ManyToOne(fetch = FetchType.LAZY)
     * 
     * @JoinColumn(name = "UserId", nullable = false) public Users getUsers() {
     * return this.users; }
     * 
     * public void setUsers(Users users) { this.users = users; }
     */

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public boolean isAdmin() {
        return this.admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public String getTelephoneNo() {
        return this.telephoneNo;
    }

    public void setTelephoneNo(String telephoneNo) {
        this.telephoneNo = telephoneNo;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Set<SubjectTeacherForClass> getSubjectteacherforclasses() {
        return this.subjectteacherforclasses;
    }

    public void setSubjectteacherforclasses(
            Set<SubjectTeacherForClass> subjectteacherforclasses) {
        this.subjectteacherforclasses = subjectteacherforclasses;
    }

}

私の応答は次のようになります

{

    "teacherList": [
        {
            "id": 1,
            "lastName": "Corega",
            "admin": true,
            "telephoneNo": "333333",
            "address": "str Albinii 45",
            "email": "sscac@yahoo.com",
            "firstName": "Constantin"
        },
        {
            "id": 2,
            "lastName": "Cotul",
            "admin": false,
            "telephoneNo": "333333",
            "address": "str Dorobanti 21",
            "email": "ascasa@gmail.com",
            "firstName": "Monica"
        },
        {
            "id": 3,
            "lastName": "Ramona",
            "admin": false,
            "telephoneNo": "333333",
            "address": "str. Izlazului 78",
            "email": "asc@yahoo.com",
            "firstName": "Calina"
        },
        {
            "id": 4,
            "lastName": "Cruceriu",
            "admin": false,
            "telephoneNo": "333333",
            "address": "str Muresului 92",
            "email": "c2c2@gmail.com",
            "firstName": "Alexandrina"
        },
        {
            "id": 5,
            "lastName": "Moldovan",
            "admin": false,
            "telephoneNo": "333333",
            "address": "str Fabricii 56",
            "email": "casca@yahoo.com",
            "firstName": "Camelia"
        }
    ]

}

私が得る例外は次のとおりです。

01-03 11:18:32.809: E/AndroidRuntime(16203): FATAL EXCEPTION: AsyncTask #1
01-03 11:18:32.809: E/AndroidRuntime(16203): java.lang.RuntimeException: An error occured while executing doInBackground()
01-03 11:18:32.809: E/AndroidRuntime(16203):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.lang.Thread.run(Thread.java:856)
01-03 11:18:32.809: E/AndroidRuntime(16203): Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
01-03 11:18:32.809: E/AndroidRuntime(16203):  at [Source: libcore.net.http.ChunkedInputStream@417f4378; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
01-03 11:18:32.809: E/AndroidRuntime(16203):  at [Source: libcore.net.http.ChunkedInputStream@417f4378; line: 1, column: 1]
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:127)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:483)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:438)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:236)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at com.catalog.views.MenuActivity$isConnectedTask.doInBackground(MenuActivity.java:176)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at com.catalog.views.MenuActivity$isConnectedTask.doInBackground(MenuActivity.java:1)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-03 11:18:32.809: E/AndroidRuntime(16203):    ... 4 more
01-03 11:18:32.809: E/AndroidRuntime(16203): Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
01-03 11:18:32.809: E/AndroidRuntime(16203):  at [Source: libcore.net.http.ChunkedInputStream@417f4378; line: 1, column: 1]
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
01-03 11:18:32.809: E/AndroidRuntime(16203):    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:124)

マッパーのMaven依存関係は次のとおりです。

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.9</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.9</version>
</dependency>

これについて何か考えはありますか?

4

1 に答える 1

1

修繕 !私がしたことは、「teacherList」という名前の Teachers の ArrayList を 1 つだけ持つクラスと、その配列リストのゲッターとセッターを作成することでした。それをリクエストに入れれば出来上がりです。そこからすべてがうまくいきました!

于 2015-01-23T12:08:41.133 に答える