Category Archives: JAVA

[How to Solve] RestController cannot be recognized in spring boot

I just started to learn spring boot, the first program helloworld encountered the problem that the annotations of @RestController and @RequestMapping(/hello) would report errors.

My personal solution:

1. Springboot has by default

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
         <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
 
</dependencies>

At this time, to introduce the web module, you need to add the spring-boot-starter-web module in pom.xml

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

2. Then use maven reimport to update the dependency package.

3. Invalidate and restart restart.

4. I use intellj idea, then the editor after restarting will automatically prompt to press alt+enter. If not prompted, manually import

import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;

It should be just fine.

Springboot introduces local jar package to deploy to server error [How to Solve]

1. Introduce the local jar package method in pom.xml

<dependency> 
    <groupId>com.arcsoft.face</groupId> 
    <artifactId>arcsoft-sdk-face</artifactId> 
    <version>3.0.0.0</version> 
    <scope>system</scope> 
    <systemPath>${ project.basedir}/src/main/resources/lib/linux-arcsoft-sdk-face-3.0.0.0.jar</systemPath> 
</dependency>

Description: systemPath specifies the path to the jar package

2. The important thing to add to the following configuration is the red configuration

<plugins> 
   <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <configuration> 
         <includeSystemScope>true</includeSystemScope>
       </configuration> 
   </plugin > 
</plugins> 

Note: includeSystemScope means that when maven is packaged, it will package the imported jar package (such as adding an external jar package in the root directory or under the resource file) into the project jar, and the project can be run on the server. Do not add this Configuration, it can run locally, because the external package can be found under lib locally, but there is no jar on the server.

[How to Solve] Laravel Ajax request error: 419 unknown status

Guide: use the laravel framework to return the error page when using Ajax for image upload request, and prompt 419 unknown status solution

419 unknown status has three solutions:

First, comment out CSRF validation in kernel.php, which is not recommended

'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class, 
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            // \App\Http\Middleware\VerifyCsrfToken::class, 
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            // \Silber\PageCache\Middleware\CacheResponse::class,
            // \App\Http\Middleware\VistLog::class
        ],

Second: write the interface to route/API. PHP

The third method is to add in the request parameters when uploading the request_ Token parameter

t'_token':'{{csrf_token()}}'

No JVM could be found on your system [How to Solve]

Please indicate the source of reprint, otherwise the copyright will be investigated according to law

When installing Android studio, an error is reported:

Error launching android Studio

No JVM installation found. Please install a 64-bit JDK.

if you already have a JDK installed, defined a JAVA_HOME wariable in

Computer > System Properties > System settings > Environment

variables.

But I have installed JDK and configured Java_ The environment variable of home, and the input Java – version and javac under CMD are all normal. The reason for the error is that there is no proper JRE (Java running environment) installed. I am a 64 bit win7 system

Because Android studio is an integration, there is no need to install SDK or ADT like eclipse. Please check whether the following steps have been completed. If they have been completed, the problem will be solved

1) The configuration of java development environment includes two steps

A. install JDK

To install JDK, select the installation directory. Two installation prompts will appear during the installation process. The first time is to install JDK, and the second time is to install JRE. It is recommended that both should be installed in different folders in the same Java folder( You can’t install both in the root directory of the Java folder. If JDK and JRE are installed in the same folder, there will be an error.)

As shown in the figure below

To install JDK, you can choose the directory at will. You only need to modify the directory before the default installation directory of Java

b. Install JRE

note : the directory before installing JRE → change → Java is the same as the directory before installing JDK

JRE official download:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Click the Java icon on the left

Then click accept license agreement

Choose the type to download according to your computer class. If your computer is 32-bit, Download Windows x86, 64 bit, Download Windows x64 64 bit

By the way, some friends may not know whether their computer is 32-bit or 64 bit. At this time, you can tap systeminfo at the command prompt and find the system type. If x86 represents 32-bit, x 64: 64 bit

c. Configure environment variables

2) Install Android studio

The solution to the java.lang.ClassCastException: [B cannot be cast to java.lang.String error in kettle

Problem description: a field queried from the database is json type data, and then an error is reported in the json input step java.lang.ClassCastException: [B cannot be cast to java.lang.String

The cause of the problem: The data queried from the database is not a String type, but a binary byte array, so an error is reported during json parsing.

Solution: In the field selection step, set the field type to String type, and set Binary to Normal to Yes.

 

[Solved] java.lang.IllegalArgumentException: Cannot format given Object as a Date

In the process of date, conversion encountered this problem, very angry

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 //Set username and perform time conversion
        for (int i = 0; i < purchasedFundsList.size() ; i++) {
            purchasedFundsList.get(i).setUserName(userName);
            purchasedFundsList.get(i).setCreateTime(sdf.format(purchasedFundsList.get(i).getCreateTime()));
        }

Check the API and find a problem. The parameter of format (date) method can only be of date type, but I passed string type, so the parameter type of the method is wrong

When through a small tool conversion, the problem is solved

sdf.format(TimeUtil.StringToDate(purchasedFundsList.get(i).getCreateTime()));
    /**
     * Convert string time format to Date time format with parameter String type
     * For example string time: "2017-12-15 21:49:03"
     * converted date time: Fri Dec 15 21:49:03 CST 2017
     * @param datetime type is String
     * @return
     */
    public static Date StringToDate(String datetime){
        SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sdFormat.parse(datetime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

Solve the problem!

[Solved] Fel:Cannot find the system Java compiler. Check that your class path includes tools.jar

SpringBoot project appears after deployment “java.lang.IllegalStateException: Cannot find the system Java compiler. Check that your class path includes tools.jar” Anomalies, as shown in the figure.

The reason is that “ToolProvider.getSystemJavaCompiler()” is empty

**Solution: ** Copy the %JAVA_HOME%\lib\tools.jar file to the %JAVA_HOME%\jre\lib\ directory

[Solved] java.util.MissingResourceException: Can’t find bundle for base name db, locale zh_CN

When using bundle to load the configuration file, this error broke out:

why

The configuration file to be loaded was not found because it must be placed under the SRC directory

If you put it under the package of com.bj186.crm, you must add the package name to the pathname of the configuration file

// use bundle
    @Test
    public void test4() {
        // ResourceBundle is a tool class specifically designed to read configuration files
        // bundle can only read properties type files, only the file name is needed when reading, no suffix is needed
        // bundle also provides an iterative method to read all configurations
        ResourceBundle db = ResourceBundle.getBundle("db");
        db.getString("driver");
        Enumeration<String> keys = db.getKeys();
        while(keys.hasMoreElements()) {
            String key = keys.nextElement();
            System.out.println(key +": " + db.getString(key));
        }
    }

The solution

Move dB. Properties to SRC directory and solve the problem

In this way, the subproblem can be solved

[Solved] Zookeeperjava.net.ConnectException: Connection refused: no further information

zookeeper error: java.net.ConnectException: Connection refused: no further information

 

Error Message:
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxnSendThread.run(ClientCnxn.java:1141)2018−08−2314:29:30,700[localhost−startStop−1−SendThread(192.168.43.31:2181)][org.apache.zookeeper.ClientCnxnSocketNIO]−[DEBUG]Ignoringexceptionduringshutdowninputjava.nio.channels.ClosedChannelExceptionatsun.nio.ch.SocketChanne

 

Solution:
Write picture description here
start tomcat again and succeed.

[Solved] Java collections.sort Error: Comparison method violates its general contract!

In the sorting code, it suddenly reports: comparison method vialates its general contract! The reasons are as follows:

In short, it can be understood as follows:

After JDK7, after the implementation of the compatible interface, the following three features should be satisfied:

1. Reflexivity: the comparison result of X and Y is opposite to that of Y and X

2. Transitivity: X > y,y> z. Then x > z。

3. Symmetry: x = y, then the comparison result of X and Z is the same as that of Y and Z

My code is as follows:

xyList.sort((left, right) ->
{
	if (x > y)
		return -1;
		
	if (x < y)
		return 1;
	
	// When equal returns a random
	if ((int) (Math.random() * 2) == 1)
		return 1;
	return -1;
});	

Because when it is equal, it will always return a random result instead of a certain result. If it does not satisfy the symmetry of sorting, the above error will be reported