Category Archives: JAVA

[Solved] Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

The front-end request enters the back-end controller and reports an error [cannot serialize instance of ` Java. Lang. string ` out of start_object token]. From the error information, it is a JSON parsing error. Compare the content sent by the front-end request and the parameters received by the back-end controller to find the problem.

Content sent by the front end:

api_save(JSON.stringify({
  po: this.form
})).then(response => {
  if (response.code === 200) {
    this.$message.success('success to save')
    this.$emit('saved')
  }
}).catch(() => {
  this.$message.error('failed to save')
})

Parameters received by the back-end controller:

@PostMapping("/save")
public Return<?> save(@RequestBody Map<String, String> paramMap) {
    try {
        service.save(JSON.parseObject(paramMap.get("po"), ProgEbs.class));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Return.success("save successfully");
}

After comparison, it is found that the front-end sending request is an object and the back-end controller receiving parameters are strings. Therefore, there is an error in JSON parsing at the back-end of the controller. At this time, just adjust the front-end sending request content to JSON strings or the back-end controller receiving parameters to object objects.

The former is used here:

api_save(JSON.stringify({
  po: JSON.stringify(this.form)
})).then(response => {
  if (response.code === 200) {
    this.$message.success('save successfully')
    this.$emit('saved')
  }
}).catch(() => {
  this.$message.error('failed to save')
})

This solves the problem of error reporting, and the back-end controller can receive parameters normally.

[Solved] The swagger UI dependency is introduced into spring boot Error

problem

Description:

Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 3 were found:
	- relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
	- linkDiscovererRegistry: defined in null
	- entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

This is because the dependent version is too old. The spring boot of version 2.5.6 is used, and the swagger of version 2.2 is too old.

The solution is to update the swagger dependency or reduce the spring boot version.

[Solved] Project Startup Error: Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'captchaService' defined in ServletContext resource [/WEB-INF/captcha-context.xml]: Cannot resolve reference to bean 'imageEngine' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageEngine' defined in ServletContext resource [/WEB-INF/captcha-context.xml]: Cannot resolve reference to bean 'captchaFactory' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'captchaFactory' defined in ServletContext resource [/WEB-INF/captcha-context.xml]: Bean instantiation via constructor failed; nested exception is java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:145)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:764)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4852)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5313)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:974)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1850)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageEngine' defined in ServletContext resource [/WEB-INF/captcha-context.xml]: Cannot resolve reference to bean 'captchaFactory' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'captchaFactory' defined in ServletContext resource [/WEB-INF/captcha-context.xml]: Bean instantiation via constructor failed; nested exception is java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:145)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 30 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'captchaFactory' defined in ServletContext resource [/WEB-INF/captcha-context.xml]: Bean instantiation via constructor failed; nested exception is java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 44 more
Caused by: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.octo.captcha.image.gimpy.GimpyFactory.class$(GimpyFactory.java:30)
    at com.octo.captcha.image.gimpy.GimpyFactory.<clinit>(GimpyFactory.java:30)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271)
    ... 53 more
Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1335)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1163)
    ... 64 more

Before the project, there was no problem. It was started normally. Then check the JDK and find that the openjdk is used. Replace it with a normal JDK and the project will start normally.

Docker compose executes the YML file error [How to Solve]

ERROR: Couldn’t connect to Docker daemon at http+ docker://localhost – is it running?

$sudo docker compose up. To start with sudo, enter the password

Other explanations (used, no)

Answer: the main reason is that docker is not started in the system service mode.

Solution:

  1. Start docker servic
  2. Generate self-starting services
  3. Check the service status, Active status is: active (running)

The requested resource [/servlet] in JavaWeb is not available [How to Solve]

Problem Description: after submitting the form, an error is reported, and the servlet is unavailable

Solution:

The development tool I use is idea2020. First, check whether comments are added:

2. Through consulting materials, it is said that servlets should be configured under web.xml. Here, I run JSP to get the web page source code, but I don’t know why. I haven’t configured web.xml in previous projects

3. Solution: add the following in the web app of web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

So far, the problem has been solved.

[Solved] JAVA Run Error: Error: Could not find or load main class

Compile helloworld.java to generate the bytecode file helloworld.class, and run helloworld.class with error: could not find or load main class

[cause analysis]

The current directory is the directory where the. Class file is located. To run the. Class bytecode file, you need to add the current directory to the system variable classpath to find the Java classes in the current directory.

The classpath path is entered incorrectly. You need to enter the current path and add.:

[solution]

Step 1: add.:,. In front of the classpath system variable at the end of the profile file under/etc to represent the current path

The classpath in the original/etc/profile file is:

CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

The added classpath is: classpath =.: $Java_HOME/jre/lib/ext:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

Step 2: on the command line, enter source/etc/profile to make the system variables in/etc/profile take effect and run.

Java : Character decoding failed [How to Solve]

Error Messages: org.apache.tomcat.util.http.Parameters [DirectJDKLog.java : 173] Character decoding failed. Parameter [param] with value [] has been ignored. Note that the name and value quoted here may be corrupted due to the failed decoding. Use debug level logging to see the original, non-corrupted values.
Note: further occurrences of Parameter errors will be logged at DEBUG level.
Cause: The http interface request contains % in the parameters, and the encoding is wrong, causing the data transmission to be empty.
Solution: param.replaceAll(“%” , “%25”);

JAVA Error: MalformedURLException: unknow protocol: f

Question:

Java use URL reading file interpretation error MalformedURLException

1. Malformedurlexception: unknown protocol: F is reported when using URL object to parse the file. It’s quite confusing. Check the URL class source code

It is found that the parameter prefix needs to be “Protocol”, because the parameter I transmitted is a file under a disk. Of course, the drive letter here is not a protocol, so I decided to add it in front

File:///, the whole becomes   file:/// F://test.txt

because   “file:///” Is a standard general document protocol

2. Here is a knowledge point, that is, the HTTP protocol we usually know. The general path is http://10.0.0.1:port/xxx/xxx ,

Why is the File Protocol   ///, The difference is that the file path has no host, that is, the host IP, so it is directly omitted here

[Solved] Spring Boot Error: “jdbcUrl is required with driverClassName.”

Question:

After spring boot is upgraded to 2.0, an error is reported when configuring multiple data sources:

“Jdbcurl is required with driverclassname.” or cause: java.lang.illegalargumentexception: datasource or datasourceclassname or jdbcurl is required.] with root cause

The solution is as follows:

Spring boot 1.0 configures the data source as spring.datasource.url and spring.datasource.driverclassname.

Spring boot 2.0 needs to be changed to spring.datasource.jdbc-url and spring.datasource.driver-class-name

Note: use the default data source of spring boot 2.0, and configure the following spring.datasource.url and spring.datasource.driver-class-name.

IDEA Compile Kotlin Project Error: Cannot inline bytecode buit with JVM target 1.8 into bytecode that is being built with JVM target 1.6.Please specify proper ‘-jvm-target’ option

Reason: the target set by idea’s default kotlin compiler is 1.6, which can be modified to 1.8 or above