当前位置: 首页 > 知识库问答 >
问题:

Spring引导控制器不响应POST请求

莫乐
2023-03-14
package com.[my-domain].[application-name].controller;

[a bunch of imports]

@RestController
@CrossOrigin
public class PrintController {
    Logger logger = LoggerFactory.getLogger(PrintController.class);

    @RequestMapping(value="/cloudprint", method=RequestMethod.POST,
            headers={"Accept=application/json"})
    @ResponseStatus(HttpStatus.CREATED)
    public @ResponseBody String printPost() { 
        logger.debug("in printPost");
        return "OK";
    }

    @RequestMapping(value="/cloudprint", method=RequestMethod.GET,
            headers={"Accept=application/json"})
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody String printGet(HttpServletRequest request) {
        logger.debug("in printGet");
        return "OK";
    }

    @RequestMapping(value="/cloudprint", method=RequestMethod.DELETE,
            headers={"Accept=application/json"})
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody String printDelete() {
        logger.debug("in printDelete");
        return "OK";
    }
}

----在2019-06-03@13:21 CET下面添加信息----因为我有一个普通的Spring(非引导)应用程序接受来自打印机的POST请求,所以我能够在传入的请求中记录信息。所以我就这么做了。

这是来自打印机的POST请求之一,Spring boot Oontroller不接受它:

auth type: null
content type: application/json
-- HEADERS --
Header: host : dev.[our-domain-name].com
Header: accept : */*
Header: user-agent : Cente HTTPc
Header: content-type : application/json
Header: content-length : 303
Header: connection : keep-alive
QueryString: null
-- PARAMETERS --
END

这是Postman向完全相同的URL发送的POST请求之一,Spring boot Oontroller接受该请求:

auth type: null
cotent type: application/json
-- HEADERS --
Header: content-type : application/json
Header: cache-control : no-cache
Header: Postman-Token : caf99fa1-4730-4193-aab3-c4874273661d
Header: user-agent : PostmanRuntime/7.6.0
Header: accept : */*
Header: host : dev.[our-domain-name].com
Header: accept-encoding : gzip, deflate
Header: content-length : 0
Header: connection : keep-alive
QueryString: null
-- PARAMETERS --
END
{"status": "29 a 0 0 0 0 0 0 0 0 0 0",
 "printerMAC": "00:11:62:1b:xx:xx",
 "statusCode": "200%20OK",
 "printingInProgress": false,
 "clientAction": null,
 "display": [
   {"name": "MainDisplay" ,
    "status": {"connected": false}}],
 "barcodeReader": [
   {"name": "MainBCR" ,
    "status": {"connected": false,"claimed": false}}]}
@RequestMapping(value="/cloudprint", method=RequestMethod.POST,
        headers={"Accept=application/json"})
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody String printPost(@RequestBody PrinterPostRequest printerPostRequest,
        HttpServletRequest request) {  
    HttpRequestLogger.log(request);
    return "OK";
}

---在2019-06-05@10:16 CET下面添加信息----我有一个主意。使用Spring RestTemplate,我向引导应用程序发送了一个POST请求,其头部和负载与打印机发送的请求相同。我得到了一个org.springframework.web.client.ResourceAccessException,其中包含以下消息:

I/O error on POST request for "https://boot.[my-domain].com:8443/cloudprint":sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

共有1个答案

叶琦
2023-03-14

如果您使用Spring Boot2,那么由于CSRF保护,这种情况可能会发生。它只影响非GET请求,并且在Spring Boot2上默认打开,但在早期版本中关闭。

所以它很好地反映了你的问题--描述--尽管我不完全明白它如何通过邮递员工作...然而,它有可能以某种方式自动处理它...

无论如何,值得一试:

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
  }
}
 类似资料:
  • 我是Spring Boot的新手,并试图构建一个简单的Web应用程序。我定义了一个包含我的url映射的控制器类,但在浏览器上它给我一个白标页面错误(404)。我无法理解为什么它无法映射。我尝试过更改我的组件扫描基础包,但它仍然没有将我重定向到控制台中的页面“abc”或打印“在控制器中”。 pom.xml 控制器类 主类

  • 我有一个spring应用程序,它可以与Mobile交换JSON。Spring控制器如下所示: 我想知道,记录http请求正文和响应正文的最佳方式是什么?目前,我有一个定制的json消息转换器,它在从json中创建bean之前记录一个请求正文。我使用CustomTraceInterceptor记录响应正文。不幸的是,CustomTraceInterceptor不允许记录请求正文。 任何更好的解决方案

  • 我可以在POST请求时发送对象列表作为响应,并在VueJs客户端中接收它 但是当我尝试用自定义类响应时(我甚至在请求映射注释中添加了) 在axios的帮助下发送VueJs端请求。邮递 Vue用户界面客户端接收 为什么我可以用对象列表响应而不能用POJO响应,以及如何解决这个问题?非常感谢。 PS.项目依赖于jackson-数据库v2.8.2和Spring v4.3.1

  • 将响应实体主体类型更改为String非常有效。怎么了?也许这是窃听器? 附注:使用Spring4.2.8,Spring靴1.3.8。

  • > 我有两个控制器(ControllerA和ControllerB) 两个控制器都调用一个服务(MyService)。 MyService调用名为MyRepository的接口,该接口有两个实现(FirstRepository和SecondRepository)。 如何可能在从ControllerA调用服务(MyService)时使用FirstRepository而在调用来自ControllerB

  • 我试图找出一种最简单的方法来控制基本Spring Boot RESTful服务的404 Not Found处理程序,例如Spring提供的示例: https://spring.io/guides/gs/rest-service/ 而不是让它返回默认的Json输出: 或者有比抛出并处理NoHandlerFoundException更好的方法吗?