gpt4 book ai didi

java - 方法 POST 不支持 Netflix Eureka 和 ZUUL

转载 作者:行者123 更新时间:2023-12-02 06:11:08 27 4
gpt4 key购买 nike

我正在用 JAVA 创建一个 Rest Controller 。当我在本地运行应用程序时,我可以执行 POST 操作。然后我创建一个 JAR,然后将其部署在服务器上。请注意,我使用 Netflix Eureka 进行服务发现,并使用 zuul 作为 API 网关。应用程序开始在服务器上正常运行,并且也在 Eureka 服务器中注册。但是当我使用 POST 服务时,它给我错误:405 Method Post not support 。

Controller 类

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 方法施加任何限制。以下是我编写的示例 Controller 代码,在 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

关于java - 方法 POST 不支持 Netflix Eureka 和 ZUUL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com