[AngularJS] [JavaEE]如何使用$ http.post()发送多个数据

问题描述 投票:1回答:2

我在webapp上工作,我有一个问题,发送数据,我没有找到一些例子。我使用AngularJS和JavaEE。

现在:

AngularJS:

测验是一个对象。

 roomFactory.create = function(quiz){
    //item.idLesson = $stateParams.lessonid;
    return $http.post("/api/private/room/create",quiz)
        .then(function (response){
            roomFactory.room = response.data;
            return roomFactory.room;
        },function (response){
            $q.reject(response);
        });
};

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //get the identity of the one who send the request
    Map<String, Object> payload = AppConfig.getRequestPayload(request);
    //objetMapper --> use to add request....
    ObjectMapper objectMapper = new ObjectMapper();

    Random randomGenerator;
    // Number of question for the two sets
    int nbQuestion = 0;
    List<Question> questionsRandom = new ArrayList<>();

    //Get object from request

    //List<String> list = objectMapper.readValue(AppConfig.getJsonRequest(request), new TypeReference<List<String>>(){});
    //List<Quiz> list2 = objectMapper.readValue(AppConfig.getJsonRequest(request), new TypeReference<List<Quiz>>(){});
    //String name = list.get(0);

    Quiz quiz = objectMapper.readValue(AppConfig.getJsonRequest(request),Quiz.class);
    if (quiz.getDuration() == null) {
        quiz.setDuration(-1);
    }

    //LOGGER.info("getIdLesson : ");
    //Get list of question from datastore
    List<Question> questions = ofy().load().type(Question.class).filter("idLesson", quiz.getIdLesson()).list();

    //Take some questions from the list to make the quiz
    if (!questions.isEmpty()) {
        if (questions.size() < quiz.getNbQuestion()) {
            nbQuestion = questions.size();
        } else {
            nbQuestion = quiz.getNbQuestion();
        }
        // we peek all the question randomly to the server and create a list of question
        while (nbQuestion > 0) {
            randomGenerator = new Random();
            int index = randomGenerator.nextInt(questions.size());
            questionsRandom.add(questions.get(index));
            questions.remove(questions.get(index));
            nbQuestion--;
        }

        if (!questionsRandom.isEmpty()) {

            //Set the quiz
            quiz.setQuestions(questionsRandom);
            quiz.setNbQuestion(questionsRandom.size());
            Lesson lesson = ofy().load().type(Lesson.class).id(Long.valueOf(quiz.getIdLesson())).now();
            //Lesson lesson = ofy().load().type(Lesson.class).filter("idLesson", quiz.getIdLesson()).first().now();


            //SET the room
            //User user = ofy().load().type(User.class).id(Long.valueOf(jsonUserId.toString())).now();

            User user = ofy().load().type(User.class).filter("email", payload.get("email").toString()).first().now();

            //LOGGER.info("User : "+user.getFirstName());

            Room room = new Room(user, quiz, 60);
            room.calculTimeToFinishTheQuiz();
            room.setName(lesson.getTitle() + RoomManager.roomNumber);
            room.setId(quiz.getIdLesson() + RoomManager.roomNumber);

            //Save the room in RoomManager
            RoomManager.roomNumber++;
            RoomManager.addNewRoom(quiz.getIdLesson(), room);


            //Send the room in response
            String json = objectMapper.writeValueAsString(room);
            response.setContentType("application/json");
            response.getWriter().write(json);
        }
    }
}

}

我需要在我的fonction中创建另一个参数:

roomFactory.create = function(quiz, roomName){

我试着这个发送两个数据:

return $http.post("/api/private/room/create",quiz, roomName)

要么

return $http.post("/api/private/room/create",[quiz, roomName])

在Servlet中获取数据:

第一个解决方

String roomName= objectMapper.readValue(AppConfig.getJsonRequest(request),String.class);

二解决方案:

List<String> list = objectMapper.readValue(AppConfig.getJsonRequest(request), new TypeReference<List<String>>(){});

    String roomName = list.get(0);
    roomName = roomName.replace("\"", "");

但第二个没有用,因为测验是一个对象。我尝试转换测验,但我没有工作。

javascript angularjs servlets java-ee-6
2个回答
1
投票

您可以传递多个数据,如下所示

var Indata = {'pram1': 'value1', 'pram2': 'value2' };
$http.post("/api/private/room/create", Indata)
.then(function (response){
        roomFactory.room = response.data;
        return roomFactory.room;
    },function (response){
        $q.reject(response);
    });

这是更好的example


0
投票

为了在json中发送多个列表来休息api,请使用此代码

var settings1 = {pram1: 'value1', pram2: 'value2',pram3: 'value3'};
var settings2 = {pram1: 'value1', pram2: 'value2',pram3: 'value3'};
var settings3 = {pram1: 'value1', pram2: 'value2',pram3: 'value3'};
var listObj = [settings1,settings2,settings3];
$http.post("/api/private/room/create",listObj)
.then(function (response){
    roomFactory.room = response.data;
    return roomFactory.room;
},function (response){
    $q.reject(response);
});

并在控制器中

@RequestMapping(value = "/api/private/room/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity createRequest(@RequestBody List<SettingsModel> settingsList) {

}

SeetingsModel类是这样的

public class SettingsModel {
    private String param1;
    private String param2;
    private String param3;
    public String getParam1() {
        return param1;
    }
    public void setParam1(String param1) {
        this.param1 = param1;
    }
    public String getParam2() {
        return param2;
    }
    public void setParam2(String param2) {
        this.param2 = param2;
    }
    public String getParam3() {
        return param3;
    }
    public void setParam3(String param3) {
        this.param3 = param3;
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.