helloworld (springmvc non-maven)
Environmental preparation
windows environment
- intellij idea ultimate 2018.1
- jdk1.8
- springmvc 4.3.18
- fiddler (simulate sending packets)
start
step1 initial project structure
step2
finish
step3 The project structure is roughly as follows
Compared to native servlet programs
- 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
step5 Run and deploy to the tomcat server
Run->Edit configuration
select local
Click the + button to add artifact
start after apply
At this time, tomcat usually starts to report an error
The reason is that the jar package is not imported
Solution:
File->Project Structure->Artifacts
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
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
and selected the corresponding three jackson jar packages copied to the lib folder in advance.
after
Switch to the Artifacts tab, pay attention
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
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)
@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:
- 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
- The extended markdown syntax of csdn is very powerful, such as tables, footnotes, embedded HTML , etc.