尝试在 POSTMAN 中获取一个主题 http://localhost:8080/topics?id=1。收到以下错误

Posted

技术标签:

【中文标题】尝试在 POSTMAN 中获取一个主题 http://localhost:8080/topics?id=1。收到以下错误【英文标题】:Trying to GET one topic http://localhost:8080/topics?id=1 in POSTMAN. Got the following error 【发布时间】:2019-08-11 09:34:16 【问题描述】:

连接只读模式在连接建立后不可强制执行。 要强制使用只读连接,请设置只读数据源或连接属性。 ERRORCODE=4474,SQLSTATE=01000

这是我的控制器

package com.course.springbootstarter.topic;

import java.util.List;
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.RestController;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
public class TopicController 

    @Autowired
    private TopicService topicService;

    @RequestMapping(method = RequestMethod.GET, value = "/topics")
    public List<Topic> getAllTopics() 
        return topicService.getAllTopics();
    

    @RequestMapping(method = RequestMethod.GET, value = "/topics/id")
    public Topic getTopic(@PathVariable long id) 
        return topicService.getTopic(id);
    

    @RequestMapping(method = RequestMethod.POST, value = "/addtopic")
    public void addTopic(@RequestBody Topic topic) 
        topicService.addTopic(topic);
    

    @RequestMapping(method = RequestMethod.PUT, value = "/updatetopic/id")
    public void updateTopic(@RequestBody Topic topic, @PathVariable long id) 
        topicService.updateTopic(id, topic);
    



Topic.java

package com.course.springbootstarter.topic;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "course")
public class Topic 

    private static final long serialVersionUID = -3009157732242241606L;
    @Id
    //@GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private String description;

    public Topic() 

    

    public Topic(long id, String name, String description) 
        super();
        this.id = id;
        this.name = name;
        this.description = description;
    

    public long getId() 
        return id;
    

    public void setId(long id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getDescription() 
        return description;
    

    public void setDescription(String description) 
        this.description = description;
    


我收到此错误消息:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

2019-03-20 10:43:39.947  INFO 10740 --- [           main] c.course.springbootstarter.CourseApiApp  : Starting CourseApiApp on IRL103021 with PID 10740 (C:\Users\c166422\Documents\github\springboot-restful-mysql-master\target\classes started by c166422 in C:\Users\c166422\Documents\github\springboot-restful-mysql-master)
2019-03-20 10:43:39.953  INFO 10740 --- [           main] c.course.springbootstarter.CourseApiApp  : The following profiles are active: demo
2019-03-20 10:43:40.156  INFO 10740 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@f61c7b6: startup date [Wed Mar 20 10:43:40 CDT 2019]; root of context hierarchy
2019-03-20 10:43:43.564  INFO 10740 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$45faa968] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-20 10:43:45.279  INFO 10740 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-03-20 10:43:45.307  INFO 10740 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2019-03-20 10:43:45.309  INFO 10740 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2019-03-20 10:43:45.557  INFO 10740 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-20 10:43:45.560  INFO 10740 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5408 ms
2019-03-20 10:43:46.161  INFO 10740 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-20 10:43:46.201  INFO 10740 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-03-20 10:43:46.211  INFO 10740 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-03-20 10:43:46.213  INFO 10740 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-03-20 10:43:46.220  INFO 10740 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-03-20 10:43:47.813  INFO 10740 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2019-03-20 10:43:47.895  INFO 10740 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
2019-03-20 10:43:48.233  INFO 10740 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core 5.0.11.Final
2019-03-20 10:43:48.239  INFO 10740 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-03-20 10:43:48.248  INFO 10740 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2019-03-20 10:43:48.428  INFO 10740 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
2019-03-20 10:43:48.776  INFO 10740 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.DB2Dialect
2019-03-20 10:43:49.896  INFO 10740 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2019-03-20 10:43:50.052  INFO 10740 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-03-20 10:43:51.515  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@f61c7b6: startup date [Wed Mar 20 10:43:40 CDT 2019]; root of context hierarchy
2019-03-20 10:43:51.783  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/topics/id],methods=[GET]" onto public com.course.springbootstarter.topic.Topic com.course.springbootstarter.topic.TopicController.getTopic(long)
2019-03-20 10:43:51.788  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/addtopic],methods=[POST]" onto public void com.course.springbootstarter.topic.TopicController.addTopic(com.course.springbootstarter.topic.Topic)
2019-03-20 10:43:51.791  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/updatetopic/id],methods=[PUT]" onto public void com.course.springbootstarter.topic.TopicController.updateTopic(com.course.springbootstarter.topic.Topic,long)
2019-03-20 10:43:51.795  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/topics],methods=[GET]" onto public java.util.List<com.course.springbootstarter.topic.Topic> com.course.springbootstarter.topic.TopicController.getAllTopics()
2019-03-20 10:43:51.823  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2019-03-20 10:43:51.844  INFO 10740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2019-03-20 10:43:51.974  INFO 10740 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-03-20 10:43:51.976  INFO 10740 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-03-20 10:43:52.125  INFO 10740 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-03-20 10:43:52.822  INFO 10740 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-03-20 10:43:53.509  INFO 10740 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-03-20 10:43:53.663  INFO 10740 --- [           main] c.course.springbootstarter.CourseApiApp  : Started CourseApiApp in 14.767 seconds (JVM running for 21.743)
2019-03-20 10:44:03.727  INFO 10740 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-03-20 10:44:03.731  INFO 10740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2019-03-20 10:44:03.820  INFO 10740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 88 ms
2019-03-20 10:44:03.976  INFO 10740 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2019-03-20 10:44:04.433  WARN 10740 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 4474, SQLState: 01000
2019-03-20 10:44:04.434  WARN 10740 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : [jcc][t4][10217][10310][4.19.66] Connection read-only mode is not enforceable after the connection has been established.
To enforce a read only connection, set the read-only data source or connection property. ERRORCODE=4474, SQLSTATE=01000

【问题讨论】:

我认为您需要向我们展示您的 application.properties(也适用于您从这里开始的环境)。并告诉我们您尝试过什么来解决这个问题?而且,还有 - 您使用的是什么数据库,它位于哪里? spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver spring.datasource.url=jdbc:db2://lddb2data00001:50002/COURSESCHEM spring.datasource.username= db2user spring.datasource.password=db2password spring.jpa.generate-ddl=true spring.mvc.view.prefix:/spring.mvc.view.suffix:.jsp spring.profiles.active=demo 另见:www-01.ibm.com/support/docview.wss?uid=swg21685228 将其命名为“警告”而不是“错误”。 【参考方案1】:

通过将此行添加到 log4j.properties 来解决问题。感谢@xerx593

log4j.logger.org.hibernate.engine.jdbc.spi.SqlExceptionHelper=ERROR 

【讨论】:

欢迎,兄弟! :) (澄清:第一行将特定记录器/类别/(类)名称的(级别阈值设置为ERROR)(请参阅您的原始日志!)。第二行通常设置(...)(完整的“休眠日志”))。 根据给定的事实,我“非常确定”第二行有效!要仅测试第一行,请尝试“独立”(不带/注释第二行)。 @xerx593,你是对的!我只需要这一行来解决这个问题 log4j.logger.org.hibernate.engine.jdbc.spi.SqlExceptionHelper=ERROR 为了澄清:您最初(注释)的记录器名称似乎是指较旧的休眠版本:log4j.logger.org.hibernate.util.JDBCExceptionReporter=...,我通过“类名”找到了实际的记录器(它是“非默认”但在您的帖子(日志)中通过(完全限定的)类名来命名记录器的“常见做法”:2019-03-20 10:44:04.433 WARN 10740 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning...【参考方案2】:

可以忽略此消息。我没有发现它对应用程序有任何影响。但是,如果您希望禁止显示此警告

    您可以更改记录器级别。 您可以取消所有警告

更多信息请关注Link

【讨论】:

# 根记录器选项 log4j.rootLogger=INFO, file # 将日志消息定向到日志文件 log4j.appender.file=org.apache.log4j.RollingFileAppender #重定向到 Tomcat 日志文件夹 #log4j.appender .file.File=$catalina.home/logs/logging.log log4j.appender.file.File=C:\\logigng.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss %-5p %c1:%L - %m%n log4j.logger.org.hibernate.util.JDBCExceptionReporter=错误 我尝试通过在 log4j.properties 中添加这个 log4j.logger.org.hibernate.util.JDBCExceptionReporter=ERROR 来抑制所有警告,但仍然收到相同的错误消息。建立连接后,连接只读模式不可执行。要强制使用只读连接,请设置只读数据源或连接属性。 ERRORCODE=4474,SQLSTATE=01000 尝试:log4j.logger.org.hibernate.engine.jdbc.spi.SqlExceptionHelper 甚至 log4j.logger.org.hibernate(它可以“分层”、“按记录器名称/类别”工作)、@Titi log4j.logger.org.hibernate.engine.jdbc.spi.SqlExceptionHelper log4j.logger.org.hibernate 的价值是什么,你能写一个完整的答案吗? @xerx593 @Titi ...实际上 我喜欢 /我很满意,这里有这个答案(对你原来的问题)! (+1)..我的意思是/听起来不错:= ERROR。有关“休眠日志配置”的更多详细信息,我会推荐您this nice answer。

以上是关于尝试在 POSTMAN 中获取一个主题 http://localhost:8080/topics?id=1。收到以下错误的主要内容,如果未能解决你的问题,请参考以下文章

技术干货 -postman接口测试获取HTTP请求

Woocommerce API Post 方法不适用于通过 POSTMAN 的 HTTP 请求 - 获取无效签名

HTTP 请求在 Postman 中有效,但在 C# 代码中无效

接口测试-postman篇

postman接口测试----1获取学生信息

Postman Interceptor 获取浏览器请求