In Solon Mvc, the @Mapping annotation is usually used in conjunction with @Controller and @Remoting for request path mapping.and, it is only supported on public functions or classes.。
1. Annotation properties
causality | clarification | note |
---|---|---|
value | trails | Alias to path |
path | trails | Alias to value |
method | Request method qualification (def=all) | usability@Post 、@Get etc. annotations instead of this attribute |
consumes | Specify the type of submission content to process the request | usability@Consumes annotation replaces this attribute |
produces | Specify the type of content to be returned | usability@Produces annotation replaces this attribute |
multipart | Declare support for multi-split requests (def=false) | If false, the automatic recognition of the |
When method=all, the request is unqualified.
2. Supported path mapping expressions
notation | clarification | typical example |
---|---|---|
** |
Any character, unlimited number of paragraphs |
** maybe/user/**
|
* |
arbitrary character | /user/* |
? |
not essential | /user/? |
/ |
Path fragment start and interval characters |
/ maybe/user
|
{name} |
Path variable declaration | /user/{name} |
Path combinations (controller mapping and action mapping) and application examples:
import ;
import ;
@Mapping("/user") // or "user", which automatically starts with "/".
@Controller
public void DemoController{
@Mapping("") //=/user
public void home(){ }
@Mapping("/") //=/user/, there is a difference from the above.
public void home2(){ }
@Mapping("/?") //=/user/ or /user, which is different from the above.
public void home3(){ }
@Mapping("list") //=/user/list, the interval will automatically fill in the "/".
public void getList(){ }
@Mapping("/{id}") //=/user/{id}
public void getOne(long id){ }
@Mapping("/ajax/**") //=/user/ajax/**
public void ajax(){ }
}
Reminder: a@Mapping
The function does not support mapping of multiple paths
3、Parameter injection
Injectable objects for non-requested parameters:
typology | clarification |
---|---|
Context | Request context () |
Locale | Requested locale information, required for internationalization |
ModelAndView | Model and View Objects () |
Supports automatic conversion of request parameters for injection:
- When the variable name has a corresponding request parameter (i.e., there is a request parameter with a name to match)
- will directly attempt to type convert the request parameter values
- When the variable name does not have a corresponding request parameter
- When the variable is an entity: it will try to inject all request parameters as attributes
- Otherwise inject null
Supports many forms of direct injection of request parameters:
- queryString
- form-data
- x-www-form-urlencoded
- path
- json body
The queryString, form-data, x-www-form-urlencoded, path parameters, support () interface to get unified.
import ;
import ;
import ;
import ;
@Mapping("/user")
@Controller
public void DemoController{
// Injectable object for non-request parameters
@Mapping("case1")
public void case1(Context ctx, Locale locale , ModelAndView mv){ }
// request parameters (can be bulk; supports queryString, form; json, or other serialized formats supported)
@Mapping("case2")
public void case2(String userName, String nickName, long[] ids, List<String> names){ }
// request parameters (can be a fully loaded structure; supports queryString, form; json, or other supported serialized formats Mapping)
@Mapping("case3")
public void case3(UserModel user){ }
// It can also be a mashup
@Mapping("case4")
public void case4(Context ctx, UserModel user, String userName){ }
//file upload //note with <input type='file' name="file1" /> name matching
@Mapping("case5")
public void case5(UploadedFile file1, UploadedFile file2){ }
// Multiple file uploads with the same name
@Mapping("case6")
public void case6(UploadedFile[] file){ }
}
Reminder:?user[name]=1&ip[0]=a&ip[1]=b&=1
The style of parameter injection requires the introduction of a plugin:solon-serialization-properties
4. Parameter injection with annotations
Annotation:
explanatory note | clarification |
---|---|
@Param | Injects request parameters (including: query-string, form). Acts as a way to specify names, default values, etc. |
@Header | Injection request header |
@Cookie | Inject request cookie |
@Path | Inject the request path variable (since the framework handles this automatically, this just identifies it for documentation purposes) |
@Body | Inject the request body (usually handled automatically. Only needed for String, InputSteam, Map of body) |
Annotation Related Properties.
causality | clarification | Applicable Notes |
---|---|---|
value | parameter name | @Param, @Header, @Cookie, @Path |
name | Parameter name (alias to value) | @Param, @Header, @Cookie, @Path |
required | mandatory | @Param, @Header, @Cookie, @Body |
defaultValue | default value | @Param, @Header, @Cookie, @Body |
import ;
import ;
import ;
import ;
import ;
@Mapping("/user")
@Controller
public void DemoController{
@Mapping("case1")
public void case1(@Body String bodyStr){ }
@Mapping("case2")
public void case2(@Body Map<String,String> paramMap, @Header("Token") String token){ }
@Mapping("case3")
public void case3(@Body InputStream stream, @Cookie("Token") token){ }
//Does this use case add @Body have the same effect
@Mapping("case4")
public void case4(@Body UserModel user){ }
@Mapping("case5/{id}")
public void case5(String id){ }
//If the name is different,That's why it's necessary. @Path //Otherwise it's an automatic process(as above)
@Mapping("case5_2/{id}")
public void case5_2(@Path("id") String name){ }
@Mapping("case6")
public void case6(String name){ }
//If the name is different,That's why it's necessary. @Param //Otherwise it's an automatic process(as above)
@Mapping("case6_2")
public void case6_2(@Param("id") String name){ }
//If you want the default value,That's why it's necessary. @Param
@Mapping("case6_3")
public void case6_3(@Param(defaultValue="world") String name){ }
@Mapping("case7")
public void case7(@Header String token){ }
@Mapping("case7_2")
public void case7_2(@Header String[] user){ } //v2.4.0 aftercare support
}
5. Limitation of the manner in which requests are made
One or more @Mppaing annotations can be added to qualify the request method (Unlimited, then all request methods are supported)
Request method qualification annotations | clarification |
---|---|
@Get | Restricted to Http Get requests |
@Post | Restricted to Http Post request method |
@Put | Limit to Http Put request method |
@Delete | Restricted to Http Delete request method |
@Patch | Restricted to Http Patch request method |
@Head | Restricted to Http Head request method |
@Options | Restricted to Http Options request method |
@Trace | Limit to Http Trace request method |
@Http | Limit to all Http request methods |
@Message | Restricted to the Message request method |
@To | Label the forwarding target |
@WebSokcet | Restricted to WebSokcet request method |
@Sokcet | Restricted to Sokcet request method |
@All | Allow all request methods (default) |
Other qualified annotations | clarification |
---|---|
@Produces | Affirmation of the type of output content |
@Consumes | Declare the input content type (when the output content type is not wrapped in @Consumes, the response is a 415 status code) |
@Multipart | Explicitly asserts support for Multipart inputs |
Example:
import ;
@Mapping("/user")
@Controller
public void DemoController{
@Get
@Mapping("case1")
public void case1(Context ctx, Locale locale , ModelAndView mv){ }
// It is also possible to qualify directly with Mapping properties. But it doesn't look as good as using annotations
@Mapping(path = "case1_2", method = )
public void case1_2(Context ctx, Locale locale , ModelAndView mv){ }
@Put
@Message
@Mapping("case2")
public void case2(String userName, String nickName){ }
// If there is no output assertion, the side string output defaults to "text/plain"
@Produces(MimeType.APPLICATION_JSON_VALUE)
@Mapping("case3")
public String case3(){
return "{code:1}";
}
//// can also be qualified directly using Mapping's attributes. But it looks good without using annotations
@Mapping(path="case3_2", produces=MimeType.APPLICATION_JSON_VALUE))
public String case3_2(){
return "{code:1}";
}
// If there is no output declaration, the side object output defaults to "application/json".
@Mapping("case3_3")
public User case3_3(){
return new User(); }
}
}
6. Output type
@Mapping("/user")
@Controller
public void DemoController{
// Output the view and model, rendered by the backend and then output the final format
@Mapping("case1")
public ModelAndView case1(){
ModelAndView mv = new ModelAndView();
("name", "world");
("");
return mv; }
}
// Output the structure, by default it will be rendered in josn format and outputted
@Maping("case2")
public UserModel case2(){
return new UserModel();
}
// Output the downloaded file
@Maping("case3")
public Object case3(){
return new File(...) ; //or return new DownloadedFile(...) ;//Or return new DownloadedFile(...)
}
}
7. Parent class inheritance support
@Mapping("user")
public void UserController extends CrudControllerBase<User>{
}
public class CrudControllerBase<T>{
@Post
@Mapping("add")
public void add(T t){
...
}
@Delete
@Mapping("remove")
public void remove(T t){
...
}
}