簡體   English   中英

方法POST不支持Netflix Eureka和ZUUL

[英]Method POST not supported Netflix Eureka and ZUUL

我在JAVA中創建一個Rest控制器。 在本地運行應用程序時,我可以執行POST操作。 然后,我創建一個JAR,然后將其部署在服務器上。 請注意,我正在使用Netflix Eureka進行服務發現,並將zuul用作API網關。 該應用程序在服務器上開始正常運行,並且也在Eureka服務器中注冊。 但是,當我使用POST服務時,它給我錯誤:405方法不支持發布。

控制器類

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bp.budgetpulse.domain.FeedBackEmployeeDetails;
import com.bp.budgetpulse.service.FeedBackService;


@RestController
@RequestMapping(value = "/api/v1")
public class FeedBackController {

    @SuppressWarnings("unused")
    private final Logger logger = LoggerFactory.getLogger(FeedBackService.class);

    @Autowired
    private FeedBackService feedbackService;

    /**
     * This method to save the feedback details
     * 
     * @param feedbackDetails
     * @param userName
     * @return response
     */
    @RequestMapping(value = "/saveEmployeeFeedbackDetails", method = RequestMethod.POST)
    public String saveEmployeeFeedbackDetails(@RequestBody FeedBackEmployeeDetails empFeedbackDetails) {
        return feedbackService.saveEmployeeFeedbackDetails(empFeedbackDetails);
    }

    /**
     * 
     * this method to get the feedback details
     * 
     * @return feedback details
     */
    @RequestMapping(value = "/getFeedBackDetails/{email}", method = RequestMethod.GET)
    public FeedBackEmployeeDetails getFeedBackDetails(@PathVariable String email) {
        return feedbackService.getFeedBackDetails(email);
    }
}

Netflix Eureka和ZUUL支持POST方法。 Netflix Eureka和ZUUL對API方法沒有任何限制。 這是我編寫的示例控制器代碼,可在Netflix Eureka和ZUUL環境中正常工作:

import com.aj.gradingservice.model.Grade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/")
public class GradeController {

    private static final Logger logger = LoggerFactory.getLogger(GradeController.class);

    @RequestMapping(value = "ping", method = RequestMethod.GET)
    public ResponseEntity<Map<String, String>> ping() {
        Map<String, String> response = new HashMap<>();
        response.put("message", "pong");
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    @RequestMapping(value = "grades", method = RequestMethod.GET)
    public ResponseEntity<List<Grade>> getGrades() {
        logger.info("In GradeController.getGrades(), fetching list of grades");
        List<Grade> grades = new ArrayList<>();
        grades.add(new Grade(1, "P001", "A+"));
        grades.add(new Grade(2, "C001", "A"));
        grades.add(new Grade(3, "M001", "B+"));
        return new ResponseEntity<>(grades, HttpStatus.OK);
    }

    @RequestMapping(value = "grade", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Grade> createGrade(@RequestBody Grade grade) {
        logger.info("Request received is: " + grade );
        Grade gradeCreated = new Grade(grade.getId(),grade.getStudentId(),grade.getGrade());
        return new ResponseEntity<>(gradeCreated, HttpStatus.OK);
    }
}

我寫了一篇博客文章,其中詳細介紹了設置Netflix Eureka和Zuul環境的問題,並且GitHub中有端到端的工作代碼。 請檢查: http : //softwaredevelopercentral.blogspot.com/2018/02/spring-cloud-eureka-and-zuul.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM