100 questions about back-end development using springboot

  1. How to convert properties and yaml files to each other
    Install plug-ins

  2. The difference between the properties file and the yaml file
    The properties file is assigned by "." and "=", without a space before the value, and the yaml is assigned by ":", and a space is added before the value; the yaml file is indented with a space; properties only support key-value
    pairs , The yaml configuration file supports a list, and the dash indicates the list "-";
    properties does not guarantee the loading order, and yaml has a sequence;

  3. Will the program automatically read the yaml file? Do you need to configure it?

  4. The difference between restcontroller and controller

  5. Error reporting without restcontroller
    Error resolving template [], template might not exist or might not be accessible by any of the conf

  6. How does postman pass objects to the background

  7. @Requestpart,@RequestBody,@RequestParam

  8. @RequestBody is mainly used to receive the data in the json string passed from the front end to the backend (data in the request body); and the most commonly used request body to pass parameters is undoubtedly the POST request, so when using @RequestBody to receive data , usually submitted by POST. In the same receiving method of the backend, @RequestBody and @RequestParam() can be used at the same time, @RequestBody can only have one at most, and @RequestParam() can have multiple.

blog post

9. postman pass file and object parameters at the same time

  1. How to pass objects in get requests
    Just use RequestBody just like Post requests

  2. Upload and download files

  3. Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null] with root cause
    The reason is that the object calling the length method is empty, and the file name passed in was read from the database, but I forgot to save the file name in the database.

  4. Cannot call sendError() after the response has been committed
    The literal meaning is that the response was called twice, resulting in the inability to call the sendError method.

  5. No converter for [class com.seed5.ocs.model.Respond] with preset Content-Type 'application/octet-stream'
    Change the return value type of the controller to void

response.reset()

But I used response.reset doesn't seem to work.
The case is solved, response.reset()the reason why using the function does not work is

InputStream inputStream=new FileInputStream(file)
//我这里的file参数是File类型,实际应该传入String类型的path

No case was solved, so what was the reason?

  1. hot deployment

  2. 2021 IDEA discontinued compiler.automake.allow.when.app.running

  3. npm ERR! { Error: EPERM: operation not permitted, mkdir 'C:\Program Files\nodejs\node_cache\_locks'

  4. What is the parameter res in the success.function.res() callback function of the request when the front-end and back-end interfaces are connected?

  5. Cross-domain problems when front-end and back-end docking

  6. The backend returns a unified result to the frontend,
    establishes a format
    , defines common errors, enumerates
    tool classes, and writes return information

    result style
    restful style

  7. enum class

  8. Various correspondences between axios sending requests and SpringBoot receiving requests

  9. The role of the vue $ symbol
    Vue 实例还暴露了一些有用的实例属性与方法,它们都有前缀 $,以便与用户定义的属性区分开来
    is to mount the attributes on vue

  10. this.$message()

  11. Cross-domain problem
    Cross-domain resource request problem, inevitable problem of front-end and back-end separation projects
    Same domain: same protocol, same port number, same host
    CORS, set in the backend, create a new configuration class to allow cross-domain requests.

@Configuration
public class CorsConfig implements WebMvcConfigurer{
    
    
    @Override
    public void addCorsMappings(CorsRegistry registry){
    
    
        registry.addMapping("/**")//允许跨域访问的路径
                .allowedOrigins("*")//允许跨域访问的源
                .allowedMethods("POST","GET","PUT","OPTIONS","DELETE")//允许跨域访问的方法
                .maxAge(168000)//预检间隔时间
                .allowedHeaders("*")//允许头部设置
               .allowCredentials(true);//是否允许发送cookie
    }
}

Modify the response header
jsonp
and report a line error.
When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
This is because the AllowedOrigin setting has changed when springboot is upgraded to 2.4.0 or above, and there cannot be "*"
Solution: Cross-domain configuration error, just replace .allowedOrigins with .allowedOriginPatterns.

@Configuration
public class CorsConfig implements WebMvcConfigurer{
    
    
    @Override
    public void addCorsMappings(CorsRegistry registry){
    
    
        registry.addMapping("/**")//允许跨域访问的路径
//                .allowedOrigins("*")//允许跨域访问的源
                .allowedOriginPatterns("*")
                .allowedMethods("POST","GET","PUT","OPTIONS","DELETE")//允许跨域访问的方法
                .maxAge(168000)//预检间隔时间
                .allowedHeaders("*")//允许头部设置
               .allowCredentials(true);//是否允许发送cookie
    }
}
  1. The scope of the JS callback function sends changes.
    Use the arrow function (the network request callback function)
    this.data is equivalent to this=>name

  2. No converter for [class com.seed5.ocs.model.ReturnRestful] with preset Content-Type 'application/octet-stream'
    application/octet-stream

    When a browser requests a resource, it will determine how to display/process the data to be loaded through the content-type in the http return header. If this type of browser can support browsing, the browser will directly display the resource, such as png, jpeg , video and other formats. In some scenarios of downloading files, the server may return the file stream with Content-Type: application/octet-stream in the return header, telling the browser that this is a byte stream, and the browser The default method for streaming is to download .

    Application/octet-stream is the default for application files. It means an unknown application file, and the browser generally does not automatically execute or ask for execution. The browser will treat this type of file as if it sets the HTTP header Content-Disposition value as attachment, that is, the browser will trigger the download behavior.

    In other words, the browser does not know what type it is, nor how to display it. It only knows that it is a binary file, so when it encounters a file whose content-type is application/octet-stream, the browser will Download it directly . This type generally cooperates with another response header, Content-Disposition, which indicates in what form the content of the reply should be displayed, whether it is inline (that is, a web page or part of a web page), or downloaded and saved as an attachment. local.

    Explain the source
    So is this returned parameter also regarded as the content to be downloaded by the browser? So there is no way to convert the return parameter type to a binary type.

    The currently known and effective solution to this problem is: set the function to a void function (do not set the return value type).
    But if it is set before the response is submitted, there is no way to submit the binary stream, and if it is set afterward, an error will be reportedCannot call reset() after response has been committed

  3. File deletion
    Universal file deletion method in java

  4. springboot configures global variables

  5. log

private static final Logger log = LoggerFactory.getLogger(homeworkController.class);
  1. Rich Text Editor
    I want to build a rich text editor that can edit, save, view, modify, and delete content.

  2. The correspondence between SpringBoot + Mybatis entity class attributes and database table column names
    The problem looks like this
    insert image description here

The solution is to do a mapping.
Mybatis solves the problem of inconsistency between database field names and entity class attribute names

My solution:
Turn on the hump naming rule:
modify mybaits-config.xml
insert image description here
and insert it in the settings tag
<setting name="mapUnderscoreToCamelCase" value="true"/>

  1. How does postman view the content of the original http request and response (I want specific information such as Kangkang request first)
    Click on the console, and then click on the specific request you want to view
    insert image description here

  2. "Content type 'application/octet-stream' not supported" error occurred in the request

  3. The backend uses @RequestParam to pass files, and @RequestPart passes objects to upload objects and files at the same time. How should the frontend pass parameters?
    After I turned the json object into a blob (binary array), the backend was still crazily reporting errors.
    2023-04-06T23:42:12.512+08:00 WARN 9272 --- [nio-8443-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type com.seed5.ocs.model.Studentwork from Array value (tokenJsonToken.START_ARRAY )]
    I don’t know if it’s a parsing problem or what’s going on. When passing it, the parameter is a json object. After passing it, the parameter becomes an array. The solution is to pass the @RequestParam
    type parameter , packed into objects in the background.

  4. Turn on hump mapping
    Add this sentence in the application.properties file
    mybaits.configuration.map-underscore-to-camel-case:=true
    and report an error:

 Property 'configuration' and 'configLocation' can not specified with together

The solution
is that I already have the mybaits.xml file, so I can no longer configure it in application.properties,
but I found that I have actually configured it in the mybaits.xml file
insert image description here

Did not take effect
Reason: Manually configured database connection
Solution:

  1. After the terminal uses git clone to pull the maven project, open the project with IDEA, but the files are all .java files with yellow icons, and the blue class files cannot be displayed like new Java files, and cannot be run directly.
    solution

Guess you like

Origin blog.csdn.net/m0_51312071/article/details/129776047