helloworld (springmvc non-maven)

helloworld (springmvc non-maven)

Environmental preparation

windows environment

  1. intellij idea ultimate 2018.1
  2. jdk1.8
  3. springmvc 4.3.18
  4. fiddler (simulate sending packets)

start

step1 initial project structure
insert image description here

step2
insert image description here
finish

step3 The project structure is roughly as follows
insert image description here

Compared to native servlet programs

  1. There are two more configuration files applicationContext.xml and dispatch-servlet.xml
  • applicationContext.xml is responsible for ServletContext initialization configuration
  • dispatch-servlet.xml is responsible for the specified DispatchServlet initialization configuration

Step4 Coding and writing the corresponding controller code
Some code screenshots are as follows, details https://github.com/fqcheng220/HelloWorld2_SpringMVC
insert image description here
step5 Run and deploy to the tomcat server

Run->Edit configuration
choose local
select local

insert image description here
Click the + button to add artifact

insert image description here
start after apply

At this time, tomcat usually starts to report an error
insert image description here

The reason is that the jar package is not imported
Solution:
File->Project Structure->Artifacts

insert image description here

Click fix to add dependent jar packages

restful api

Problems encountered
1. Cannot return json

Solution: Need to add support for json parsing. The HttpMessageConverter list reference held by the default AnnotationMethodHandlerAdapter does not contain json processing classes (there are other solutions such as fastjson on the Internet, and jackson is used here). The jackson jar
insert image description here
package cannot be imported at first, try adding it to Module library, also try to add it to the project library, tomcat will report that the class cannot be found, and simply try to import the TestJar package written by myself and find that it cannot be referenced. Is there anything special about the import mechanism?
Later, it suddenly occurred to me that tomcat might need to deploy the jar package
, so I first added it to the module library
insert image description here
and selected the corresponding three jackson jar packages copied to the lib folder in advance.

insert image description here

after
insert image description here

Switch to the Artifacts tab, pay attention
insert image description here

Click fix again! ! ! ! ! ! ! ! ! ! ! ! finally normal

2. The method in the controller class cannot correctly obtain the post request input parameter
@RequestParam usage requirements

  • The request header must have Content-Type: application/x-www-form-urlencoded
  • The request body is similar to userName=123&pwd=1

insert image description here

The root cause is that the original HttpServletRequest.getParameterXXX method must require these two conditions to take effect.
Source code tracking:
DispatchServlet.doDispatch(xxxx)<-------AnnotationMethodHandlerAdapter.invokeHandlerMethod(xxx)
<-------HandlerMethodInvoker.resolveRequestParam( xxx)

insert image description here

@RequestBody uses requests

  • Content-Type in the request header: must not be empty ,
    usually application/json or application/xml, etc.

HandlerMethodInvoker.resolveRequestBody(xxx)<------- depends on whether there is an instance that can be processed in the HttpMessageConverter list held by AnnotationMethodHandlerAdapter

  • If the Content-Type request is application/json, the body must be a json string similar to { "userName": "123", "pwd": "1" }, otherwise org.springframework.http.converter.HttpMessageNotReadableException: JSON
    will be reported
    parse error:xxxxxxxx

MappingJackson2HttpMessageConverter.read exception

3. Request filtering

The HttpMessageConverter list reference held by AnnotationMethodHandlerAdapter can filter the request to determine whether to respond.
The canRead and canWrite method will filter the media types of interest (no matter what Content-Type is specified in the request, that is, the request body format and Accept, that is, the response body format). In
this example, the dispatcher-servlet.xml configuration up

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                    <value>application/xml;charset=UTF-8</value>
                </list>
            </property>
        </bean>

Summarize:

  1. The annotations (request parameters/response data types) and HttpMessageConverter used by the backend determine that the frontend needs to send a request in a specified format and can accept a response in a specified format
  2. The extended markdown syntax of csdn is very powerful, such as tables, footnotes, embedded HTML , etc.

Guess you like

Origin blog.csdn.net/weixin_41548050/article/details/84956009