对于通过POST从ReactApp发送数组到SpringBoot感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍react发送post请求,并为您提供关于Couldnottransferarti
对于通过POST从React App发送数组到Spring Boot感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍react发送post请求,并为您提供关于Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.1.RELEASE...、druid-spring-boot-starter:数据库连接池,集成到springboot、GetMapping 工作但 PostMapping 在 SpringBoot 应用程序中不起作用、java – 运行SpringBootApplication PostConstruct和PreDestroy的有用信息。
本文目录一览:- 通过POST从React App发送数组到Spring Boot(react发送post请求)
- Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.1.RELEASE...
- druid-spring-boot-starter:数据库连接池,集成到springboot
- GetMapping 工作但 PostMapping 在 SpringBoot 应用程序中不起作用
- java – 运行SpringBootApplication PostConstruct和PreDestroy
通过POST从React App发送数组到Spring Boot(react发送post请求)
我已经看到类似的问题,但没有成功。我需要将数字矩阵从Web应用程序(ReactJS)发送到Spring Boot控制器。
我尝试了很多组合,但总是会出错,我的有效载荷是:
{"rows":[[7,0,0,6,4,0,0,0,0],[9,4,0,0,0,0,8,0,0],[0,8,6,2,5,0,0,9,0],[0,0,0,0,6,8,7,3,0],[4,0,8,0,2,1,0,0,0],[0,0,3,0,0,0,1,6,4],[0,0,0,0,0,9,6,7,5],[3,9,0,0,8,5,0,1,2],[0,0,5,0,0,4,0,0,0]]}
我的反应代码是:
axios.post(''http://localhost:8090/api/check'', { rows: this.props.rows }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
我的Spring Boot控制器是:
@PostMapping(path = "/check")@CrossOrigin(origins = "http://localhost:3000")public boolean check(@RequestParam(value = "rows") final int[] array, final int row, final int col, final int num) { return true;}
我已经尝试声明@RequestParam(value = "rows[]")
或@RequestParam(value ="rows")
。而不是@RequestParam(value = "rows") final Object rows
。
但是它总是以 错误400(错误请求) 响应。
如何通过POST请求传递矩阵?
谢谢
答案1
小编典典最后,我解决了将所有参数包装在一个对象中的问题。
@JsonAutoDetectpublic class Params { private int[][] matrix; private int row; private int col; private int num; [...getters and setters]
然后在控制器的方法符号中仅声明一个参数:
@PostMapping(path = "/check") @CrossOrigin(origins = "http://localhost:3000") public boolean check(@RequestBody final Params params) { return sudokuGenerator.checkValue(params.getMatrix(), params.getRow(), params.getCol(), params.getNum()); }
至关重要的是,客户端应该传递带有其属性的对象,而没有任何类型的包装,因此采用这种方式:
axios.post(''http://localhost:8090/api/check'', { matrix: this.props.rows, "row": row - 1, "col": col - 1, "num": input.textContent})
而不是以这种方式(带有根属性“ params”):
axios.post(''http://localhost:8090/api/check'', { "params" : { matrix: this.props.rows, "row": row - 1, "col": col - 1, "num": input.textContent }})
Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.1.RELEASE...
在使用 SpringBoot 项目时报错 Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:1.5.21.RELEASE from/to default from/to default (http://maven.aliyun.com/nexus/content/groups/public): Access denied to: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-starter-parent/1.5.21.RELEASE/spring-boot-starter-parent-1.5.21.RELEASE.pom , ReasonPhrase:Forbidden. and ''parent.relativePath'' points at no local POM @ line 5, column 13
报错意思是无法去仓库 http://maven.aliyun.com/nexus/content/groups/public 下载 spring-boot-starter-parent:pom:1.5.21.RELEASEjar 包。
解决思路 1:换本地 maven 仓库地址,有可能新导入项目或新建项目导致 maven 设置 setting 文件为自带,及仓库为.m 仓库。换为自安装的 maven setting 和仓库。
结果:失败。结果分析:下载出错,跟换仓库没关。
解决思路 2:换镜像地址,网上大多这种解决方法,换个地方下载 jar 包
结果:失败。结果分析:多个镜像也下载失败,跟镜像没关,远程仓库肯定不缺这种 springboot 必须的 jar 包。
解决思路 3:换 jar 包版本
结果:失败。结果分析:跟版本没关,springboot 必须包,远程仓库肯定有
解决思路 4:删除代理。
结果:成功。结果分析:下载失败,跟网络有关,太久没动 setting 文件,没想到设置了代理,导致联网下载出错。
把设置的代理注解掉即可成功下载 jar 包。
druid-spring-boot-starter:数据库连接池,集成到springboot
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
1.如上pom.xml插入配置
2.在application.properties里配置,也可以在application.yml里配置,只不过写法不一样,但也差不多
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=oracle.jdbc.driver.OracleDriver
#spring.datasource.druid.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.druid.url=jdbc:oracle:thin:@192.168.1.11:1521:orcl
spring.datasource.druid.username=platform
spring.datasource.druid.password=platform
spring.datasource.druid.initial-size=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.maxWait=60000
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
spring.datasource.druid.minEvictableIdleTimeMillis=300000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3.开始还不了解这些属性的意思后来去挨个查了下,下面是网上资料
上面都是些常用的属性。
总结:
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。DruidDataSource支持的数据库:
理论上说,支持所有有jdbc驱动的数据库。最近发现Druid在springboot框架下有更加好用的Druid Spring Boot Starter,可以省去原本写Druid的一些配置文件或者@Configuration来配置,直接将配置写在application.yml或者application.properties里,看起来更简单一些。
GetMapping 工作但 PostMapping 在 SpringBoot 应用程序中不起作用
如何解决GetMapping 工作但 PostMapping 在 SpringBoot 应用程序中不起作用?
嗨,我在 SpringBoot 应用程序中有一个带有多种方法的 REST 控制器。我的 GET 方法工作正常,但我的 POST 方法不起作用,而是返回错误 405,该方法是不允许的。任何想法都会有所帮助;提前致谢。
@GetMapping("orders")
public List<OrderDto> getorders() {
List<OrderDto> orderDtos = new ArrayList<>();
// get all the orders using the getorders method in the OrderService class
List<Order> orders = orderService.getorders();
// convert each of the returned orders into orderDto
orders.forEach(o->{
orderDtos.add(OrderDto.from(o));
});
return orderDtos;
}
@GetMapping("lineitems")
public List<OrderLineItemDto> getorderLineItems(){
List<OrderLineItemDto> orderLineItemDtos = new ArrayList<>();
// get line items using the getorderLineItems service
List<OrderLineItem> orderLineItems = orderLineItemService.getorderLineItems();
// convert each of the returned order line items into an orderLineItemDto so that the DTO is the one that gets returned to the client
orderLineItems.forEach(ol->{
orderLineItemDtos.add(OrderLineItemDto.from(ol));
});
// return the list of lineItemDtos to the calling client.
return orderLineItemDtos;
}
@GetMapping("lineitems/{id}")
public OrderLineItem getorderLineItem(@PathVariable Long id) {
return orderLineItemService.getorderLineItem(id);
}
@PostMapping("order/add")
// @RequestMapping(value = "order",// consumes = "application/json",// method = RequestMethod.POST)
public void addOrder(@RequestBody OrderDto orderDto) {
Order order = new Order();
// convert the orderDto into order
order = Order.from(orderDto);
// use the order in the order creation service
orderService.addOrder(order);
}
Response when I invoke the REST service via postman
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
java – 运行SpringBootApplication PostConstruct和PreDestroy
我在docker容器中运行spring应用程序时遇到了麻烦(spring和docker在我的环境中都有最新版本).
我希望为应用程序类AnalysisServiceBootstrap提供健康的生命周期:在创建AnalysisServiceBootstrap之后立即使用方法start()运行初始化代码,并在销毁AnalysisServiceBootstrap之前运行方法stop()(我想在某人时运行stop()代码)停止申请).
我有以下代码:
package com.pack;
import javax.annotation.postconstruct;
import javax.annotation.PreDestroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletinitializer;
@SpringBootApplication
public class AnalysisServiceBootstrap {
// called OK on docker "start <containerId>"
@postconstruct
public void start() throws Exception {
// some init code
}
// NOT called on "docker stop <containerId>"
@PreDestroy
public void stop() {
// some destroy code
}
public static void main(String[] args) {
SpringApplication.run(AnalysisServiceBootstrap.class, args);
}
}
出于某种原因,我无法在docker stop上运行方法stop().
我尝试了stackoverflow和其他资源提供的几种方法,但所有这些方法都不适用于我.
我很乐意拥有适合您的代码(不仅仅是一些时尚建议).
这里几乎是我的docker文件:
FROM *********:6556/service-jvm
ARG SERVICE_JAR_FILE
ENV SERVICE_NAME service
ENV HTTP_PORT 603
ENV HTTPS_PORT 604
ENV SERVICE_JAR /opt/my/project/${SERVICE_JAR_FILE}
EXPOSE ${HTTP_PORT} ${HTTPS_PORT}
copY ${SERVICE_JAR_FILE} /opt/my/project/${SERVICE_JAR_FILE}
CMD java -xms1024m -Xmx1024m -dump:"/opt/my/project/dumppath" -javaagent:/opt/my/project/agent.jar -Djav.awt.headless=true -jar ${SERVICE_JAR}
但是你被邀请在这里发布你有的任何工作的docker文件.
非常感谢.
解决方法:
从文档:
docker stop
Stop one or more running containers
The main process inside the container will receiveSIGTERM
, and after a grace period,SIGKILL
通过执行docker stop,你只是在杀死java(Spring)进程.
那么Spring上下文将正确关闭的保证是什么?
在Spring应用程序中处理SIGTERM的正确方法是添加shutdown hook.
最终代码应如下所示:
package com.pack;
import javax.annotation.postconstruct;
import javax.annotation.PreDestroy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletinitializer;
@SpringBootApplication
public class AnalysisServiceBootstrap {
@postconstruct
public void start() throws Exception {
// some init code
}
@PreDestroy
public void tearDown() {
// some destroy code
}
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// write here any instructions that should be executed
tearDown();
}
});
SpringApplication.run(AnalysisServiceBootstrap.class, args);
}
}
该过程在以下问题中描述:
> How to close Spring beans properly after received a SIGTERM?
> How to handle a SIGTERM
关于通过POST从React App发送数组到Spring Boot和react发送post请求的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.1.RELEASE...、druid-spring-boot-starter:数据库连接池,集成到springboot、GetMapping 工作但 PostMapping 在 SpringBoot 应用程序中不起作用、java – 运行SpringBootApplication PostConstruct和PreDestroy等相关内容,可以在本站寻找。
本文标签: