ContactManager рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдХреЗ
рдкрд┐рдЫрд▓реЗ рд╕рдВрд╕реНрдХрд░рдг рдкрд░ рд╕реНрдпрд╛рд╣реА
рд╕реВрдЦрдиреЗ рд╕реЗ рдкрд╣рд▓реЗ , рдлреЛрди рдмрдЬ рдЙрдард╛ рдФрд░ рдореИрдВрдиреЗ рд░рд┐рд╕реАрд╡рд░ рдореЗрдВ рдПрдХ рдорд┐рддреНрд░ рдХреА рдЖрд╡рд╛рдЬрд╝ рд╕реБрдиреА, рдЬреЛ рдПрдВрдбреНрд░реЙрдЗрдб рдХреЗ рд▓рд┐рдП рд╡рд┐рдХрд╛рд╕ рдореЗрдВ рдорд╣рд╛рд░рдд рд╣рд╛рд╕рд┐рд▓ рдХрд░рдиреЗ рд▓рдЧрд╛ рдФрд░ рдПрдХ рдкрд░реАрдХреНрд╖рдг рдкрд░рд┐рдпреЛрдЬрдирд╛ рдХреА рддрд▓рд╛рд╢ рдореЗрдВ рдерд╛ рдЬрд┐рд╕ рдкрд░ рд╡рд╣ рд╡реЗрдм рд╕реЗрд╡рд╛рдУрдВ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдиреЗ рдХрд╛ рдЕрднреНрдпрд╛рд╕ рдХрд░ рд╕рдХреЗред
"рдХреБрдЫ рднреА рдЖрд╕рд╛рди рдирд╣реАрдВ рд╣реИ!" рдореИрдВрдиреЗ рдЬрд╡рд╛рдм рджрд┐рдпрд╛ред
рддреЛ рдлрд┐рд▓рд╣рд╛рд▓ рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рдХреНрдпрд╛ рд╣реИ?
рдПрдХ рд╡реЗрдм рдПрдкреНрд▓рд┐рдХреЗрд╢рди рдЬреЛ рдмреНрд░рд╛рдЙрдЬрд╝рд░ рдореЗрдВ рдЪрд▓рддрд╛ рд╣реИ, рдкреНрд░рд╛рдзрд┐рдХрд░рдг рдХреЗ рд▓рд┐рдП, рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдХреЛ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдирд╛рдо рдФрд░ рдкрд╛рд╕рд╡рд░реНрдб рджрд░реНрдЬ рдХрд░рдирд╛ рд╣реЛрдЧрд╛ред рдирд┐рдпрдВрддреНрд░рдХ рдЙрди рд╡рд┐рдЪрд╛рд░реЛрдВ рдХрд╛ рдкреНрд░рдмрдВрдзрди рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдХреЗ рд▓рд┐рдП рд╡рд┐рд╢рд┐рд╖реНрдЯ JSP рдкреГрд╖реНрда рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд░рддреЗ рд╣реИрдВред
рд╡реЗрдм рд╕реЗрд╡рд╛ рдХреЗ рдорд╛рдорд▓реЗ рдореЗрдВ, рдХреЛрдИ рд▓реЙрдЧрд┐рди рдлрд╝реЙрд░реНрдо рдирд╣реАрдВ рд╣реИ, рдХреЛрдИ рдЬреЗрдПрд╕рдкреА рдкреГрд╖реНрда рдирд╣реАрдВ рд╣реИред рдПрдХ рдареЛрд╕ HTTP: рдбреЗрдЯрд╛ рдЕрдиреБрд░реЛрдзреЛрдВ рдореЗрдВ рднреЗрдЬрд╛ рдЬрд╛рддрд╛ рд╣реИ рдФрд░ рд╡рд╛рдкрд╕ JSON (рдПрдХ рд╡рд┐рдХрд▓реНрдк - XML) рдХреЗ рд░реВрдк рдореЗрдВ рд╡рд╛рдкрд╕ рдЖ рдЬрд╛рддрд╛ рд╣реИред
рдХрд╛рд░реНрдп рдпреЛрдЬрдирд╛:
- рд╕реБрд░рдХреНрд╖рд╛ рд╕реЗрдЯрд┐рдВрдЧреНрд╕ рдЬреЛрдбрд╝реЗрдВ
- рдирд┐рдпрдВрддреНрд░рдХ рдЬреЛрдбрд╝реЗрдВ
- рдкрд░реАрдХреНрд╖рдг рдХрд░рдирд╛
"рдЕрдВрдХреЛрдВ рдкрд░ рдЕрдзрд┐рдХрд╛рд░ рдФрд░ рдЪрд▓реЛ" (рд╕реА)ред
1. рд╕реБрд░рдХреНрд╖рд╛ рд╕реЗрдЯрд┐рдВрдЧреНрд╕ рдЬреЛрдбрд╝реЗрдВред
рдмреЗрд╕рд┐рдХ рдкреНрд░рдорд╛рдгреАрдХрд░рдг рддрдВрддреНрд░ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рд╡реЗрдм рд╕реЗрд╡рд╛ рдХреЗ рд▓рд┐рдП рд╕реБрд░рдХреНрд╖рд╛ рд▓рд╛рдЧреВ рдХреА рдЬрд╛ рд╕рдХрддреА рд╣реИред рдЖрдЗрдП рджреЗрдЦреЗрдВ рдХрд┐ рд╡рд╕рдВрдд рдЗрд╕ рддрдВрддреНрд░ рдХрд╛ рд╕рдорд░реНрдерди рдХреИрд╕реЗ рдХрд░рддрд╛ рд╣реИред
рд╕рдмрд╕реЗ рдкрд╣рд▓реЗ, рдЖрдЗрдП рдЕрдиреБрд░реЛрдзреЛрдВ рдХреЗ рдкреНрд░рд╛рд░реВрдк рдХрд╛ рдирд┐рд░реНрдзрд╛рд░рдг рдХрд░реЗрдВред рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рдкрд╣рд▓реЗ рд╕реЗ рд╣реА рдмреНрд░рд╛рдЙрдЬрд╝рд░ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП URL рдХрд╛ рдПрдХ рд╕реЗрдЯ рд╣реИ, рд╣рдо рдЗрд╕реЗ рдПрдХ рдирдореВрдиреЗ рдХреЗ рд░реВрдк рдореЗрдВ рд▓реЗ рд╕рдХрддреЗ рд╣реИрдВред рдмрд╕
/ws
mnemonic рдЙрдкрд╕рд░реНрдЧ рдХреЗ рдкреАрдЫреЗ рд╡реЗрдм рд╕реЗрд╡рд╛ рдХреЗ рд▓рд┐рдП URL "рдЫреБрдкрд╛рдПрдВ"ред рдпрд╛рдиреА, рд╣рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдкрддреЛрдВ рдХреЗ рд▓рд┐рдП рдкреНрд░рдХреНрд░рд┐рдпрд╛ рдХрд░рдиреА рдЪрд╛рд╣рд┐рдП:
/ws/index
,
/ws/add
,
/ws/delete
ред рдЬрдбрд╝ рддрдХ рдкрд╣реБрдВрдЪ "
/ws
" рд╣рдо рдПрдХ рд╕реНрд╡реИрдЪреНрдЫрд┐рдХ рдирд┐рд░реНрдгрдп рджреНрд╡рд╛рд░рд╛ рдирд┐рд╖рд┐рджреНрдз рдХрд░реЗрдВрдЧреЗ, рдХреНрдпреЛрдВрдХрд┐ рдХреЛрдИ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдирд╣реАрдВ рд╣реИред
рд╕реНрдкреНрд░рд┐рдВрдЧ рдЖрдкрдХреЛ рдПрдХ рд╣реА рд╕реБрд░рдХреНрд╖рд╛ рдлрд╝рд╛рдЗрд▓ рдореЗрдВ рдХрдИ http рддрддреНрд╡реЛрдВ рдХреЛ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдХрд░рдиреЗ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддрд╛ рд╣реИред рдЗрд╕рдХрд╛ рд▓рд╛рдн рдЙрдард╛рдПрдВред рд╢реБрд░реБрдЖрдд рдореЗрдВ рдЬреЛрдбрд╝реЗрдВ:
<http realm="Contact Manager REST-service" pattern="/ws/**" use-expressions="true"> <intercept-url pattern="/ws/index*" access="hasAnyRole('ROLE_USER','ROLE_ANONYMOUS')" /> <intercept-url pattern="/ws/add*" access="hasRole('ROLE_USER')" /> <intercept-url pattern="/ws/delete/*" access="hasRole('ROLE_ADMIN')" /> <intercept-url pattern="/ws/**" access="denyAll" /> <http-basic/> </http>
рдореМрдЬреВрджрд╛ рд╕реЗрдЯрд┐рдВрдЧреНрд╕ рд╕реЗ рдкрд╣рд▓реЗ рдЗрд╕ рдЦрдВрдб рдХреЛ рдмрд╣реБрдд рд╢реБрд░реБрдЖрдд рдореЗрдВ рдЬреЛрдбрд╝рдирд╛ рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╣реИред рдЪреВрдВрдХрд┐ рдпрд╣ рдЕрдзрд┐рдХ рд╡рд┐рд╢рд┐рд╖реНрдЯ рд╣реИ, рдЗрд╕рд▓рд┐рдП рд╕рдордп рдкрд░ рдврдВрдЧ рд╕реЗ
/ws
рдЙрдкрд╕рд░реНрдЧ рдХреЗ рд╕рд╛рде рдЕрдиреБрд░реЛрдз рдХреЛ рд░реЛрдХрдиреЗ рдХреЗ рд▓рд┐рдП рдЗрд╕реЗ рдкрд╣рд▓реЗ рд╕рдВрд╕рд╛рдзрд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдПред
рдХреНрдпрд╛ рдмрджрд▓ рдЧрдпрд╛ рд╣реИ? рдмрд╣реБрдд рдХрдоред JSP рд╕реЗ рд╕рдВрдмрдВрдзрд┐рдд рд╕рдм рдХреБрдЫ рд╕рдорд╛рдкреНрдд рд╣реЛ рдЧрдпрд╛ рд╣реИ, рднреВрдорд┐рдХрд╛ рдЬрд╛рдБрдЪ рдЕрдм рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рддрдВрддреНрд░
use-expressions="true"
рдХрд░рддрд╛ рд╣реИ (рд▓реЗрдХрд┐рди рдпрд╣ рдПрдХ рдЙрджрд╛рд╣рд░рдг рдХреЗ рд░реВрдк рдореЗрдВ рдорд╣рддреНрд╡рдкреВрд░реНрдг рдирд╣реАрдВ рд╣реИ)ред
/ws
рдЙрдкрд╕рд░реНрдЧ рд╕рднреА рдЙрдкрд▓рдмреНрдз url рдореЗрдВ рдЬреЛрдбрд╝рд╛ рдЧрдпрд╛ рд╣реИ,
/ws
рд░реВрдЯ рддрдХ рдкрд╣реБрдБрдЪ рд╕рднреА рдХреЛ рдЕрд╕реНрд╡реАрдХрд╛рд░ рдХреА рдЧрдИ рд╣реИ (
denyAll
)ред рдЕрдиреНрдп SPEL рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐рдпреЛрдВ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рдЙрджрд╛рд╣рд░рдг
рдпрд╣рд╛рдВ рджреЗрдЦреЗ рдЬрд╛ рд╕рдХрддреЗ
рд╣реИрдВ ред рдореИрдВ рджреЛрд╣рд░рд╛рддрд╛ рд╣реВрдВ - рдорд╛рд╕реНрдХ рдХрд╛ рдХреНрд░рдо рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╣реИ, рд╕рдмрд╕реЗ рдЖрдо
/ws/**
рдЕрдВрддрд┐рдо рд╣реИред
realm
рд╡рд┐рд╢реЗрд╖рддрд╛ рдХреЛ рдлрд┐рд░ рд╕реЗ рд╕реБрдВрджрд░рддрд╛ рдХреЗ рд▓рд┐рдП рдЬреЛрдбрд╝рд╛ рдЬрд╛рддрд╛ рд╣реИ, рдпрд╣ рдкреНрд░рд╛рдзрд┐рдХрд░рдг рд╡рд┐рдВрдбреЛ рдореЗрдВ рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛ рдпрджрд┐ рдХреЛрдИ рдмреНрд░рд╛рдЙрдЬрд╝рд░ рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рд╕рдВрдкрд░реНрдХ рд╕реВрдЪреА рдХреЗ JSON рдХреА рдкреНрд░рд╢рдВрд╕рд╛ рдХрд░рдиреЗ рдХрд╛ рдирд┐рд░реНрдгрдп рд▓реЗрддрд╛ рд╣реИред
рдФрд░ рдлрд┐рд░ рдЕрдЧреЛрдЪрд░
<http-basic/>
рддрддреНрд╡ рдкрд╣рд▓реЗ рд╕реЗ рд╣реА рдЖрд╢реНрд░рдп рдерд╛ред рдЗрд╕ рдЫреЛрдЯреЗ рд╕реЗ рдирд┐рд░реНрдорд╛рдг рдХреЗ рдкреАрдЫреЗ, рд╕реНрдкреНрд░рд┐рдВрдЧ (рдЗрд╕рдХреА рд╕рд╛рдорд╛рдиреНрдп рд╢реИрд▓реА рдореЗрдВ) рдбреЗрд╡рд▓рдкрд░ рд╕реЗ рдЗрд╕ рдореВрд▓ рдкреНрд░рдорд╛рдгреАрдХрд░рдг рдХреЗ рддрдВрддреНрд░ рдХреА рдЬрдЯрд┐рд▓рддрд╛ рдХреЛ рдЫреБрдкрд╛рддрд╛ рд╣реИред рдФрд░ рд╣рдорд╛рд░реЗ рдкрд╛рд╕ рдЗрд╕ рдкрд░ рднрд░реЛрд╕рд╛ рди рдХрд░рдиреЗ рдХрд╛ рдХреЛрдИ рдХрд╛рд░рдг рдирд╣реАрдВ рд╣реИред рдПрдХ рдиреАрдЪреЗ, рджреЛ рдЬрд╛рдиреЗ рдХреЗ рд▓рд┐рдПред рдЖрдЗрдП рдирд┐рдпрдВрддреНрд░рдХ рдкрд░ рдПрдХ рдирдЬрд╝рд░ рдбрд╛рд▓реЗрдВред
рдПрдирдмреА рд╣рдореЗрдВ рдпрд╣ рдирд╣реАрдВ рднреВрд▓рдирд╛ рдЪрд╛рд╣рд┐рдП рдХрд┐ рд╣рдорд╛рд░реЗ рдкрд╛рд╕ 2 рд╕реБрд░рдХреНрд╖рд╛ рд╣реИрдВред рдПрдХреНрд╕рдПрдордПрд▓ рдлрд╛рдЗрд▓реЗрдВ, рдкрд░реАрдХреНрд╖рдгреЛрдВ рдХреЗ рд▓рд┐рдП рдореБрдЦреНрдп рдПрдХред рджреЛрдиреЛрдВ рдореЗрдВ рдкрд░рд┐рд╡рд░реНрддрди рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдПред
2. рдПрдХ рдирд┐рдпрдВрддреНрд░рдХ рдЬреЛрдбрд╝реЗрдВред
рд╣рдо рд╡реЗрдм рд╕реЗрд╡рд╛ рдХреЗ рд▓рд┐рдП рдирд┐рдпрдВрддреНрд░рдХ рдореЗрдВ JSP рдХреЗ рд▓рд┐рдП рдПрдХ рдирд┐рдпрдВрддреНрд░рдХ рдХреИрд╕реЗ рдмрджрд▓ рд╕рдХрддреЗ рд╣реИрдВ? рдмрд╣реБрдд рд╕рд░рд▓ рд╣реИред рдореМрдЬреВрджрд╛ рд╡рд┐рдзрд┐рдпрд╛рдБ рдирд╛рдо рджреГрд╢реНрдп рдХреЗ рд╕рд╛рде рдпрд╛ рдЖрдЧреЗ / рдкреБрдирд░реНрдирд┐рд░реНрджреЗрд╢рд┐рдд рдкреГрд╖реНрда рдХреЗ рд╕рд╛рде рдПрдХ рд╕реНрдЯреНрд░рд┐рдВрдЧ рд▓реМрдЯрд╛рддреА рд╣реИрдВ, рдЬрдмрдХрд┐ рдбреЗрдЯрд╛ (рдЕрдиреБрдмрдВрдзреЛрдВ рдХреА рд╕реВрдЪреА рдФрд░ рдЙрдирдХреЗ рдкреНрд░рдХрд╛рд░) рдореЙрдбрд▓ рд╡рд┐рд╢реЗрд╖рддрд╛рдУрдВ рдореЗрдВ рд╕реНрдерд╛рдирд╛рдВрддрд░рд┐рдд рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВред рд╡реЗрдм рд╕реЗрд╡рд╛ рд╡рд┐рдзрд┐рдпреЛрдВ рдХреЛ JSON рдореЗрдВ рдХреНрд░рдорд╛рдВрдХрди рдХреЗ рд▓рд┐рдП рдЙрдкрдпреБрдХреНрдд рд╡рд╕реНрддреБрдУрдВ рдХреЛ рд╡рд╛рдкрд╕ рдХрд░рдирд╛ рдЪрд╛рд╣рд┐рдПред рдЗрд╕рдХреЗ рдЕрд▓рд╛рд╡рд╛, POST рд╡рд┐рдзрд┐
/ws/add
рд╕реАрдзреЗ JSON рдХреЗ рд░реВрдк рдореЗрдВ рдирдП рд╕рдВрдкрд░реНрдХ рдХреЗ рдбреЗрдЯрд╛ рдХреЛ рдЕрдиреБрд░реЛрдз рдирд┐рдХрд╛рдп рдореЗрдВ рдПрдХ рд╕реНрдЯреНрд░рд┐рдВрдЧ рдХреЗ рд░реВрдк рдореЗрдВ рдкреНрд░рд╛рдкреНрдд рдХрд░реЗрдЧрд╛ред рдпрд╣ рд╡реНрдпрд╛рдкрд╛рд░ рдХрд╛ рд╕рдордп рд╣реИред
рдкреБрд░рд╛рдиреЗ рдирд┐рдпрдВрддреНрд░рдХ рдХреЗ рд╕рдорд╛рди рдкреИрдХреЗрдЬ рдореЗрдВ рдПрдХ рдирдпрд╛
ContactWsController.java
рдлрд╝рд╛рдЗрд▓ рдмрдирд╛рдПрдВред рдФрд░ рддреБрд░рдВрдд рд╡рд░реНрдЧ рд╕реНрддрд░ рдкрд░, рд╣рдо рдЕрдкрдиреЗ рджрд╛рд╡реЛрдВ рдХреЛ рдЗрдВрдЧрд┐рдд рдХрд░рддреЗ рд╣реИрдВ рдХрд┐ рдпрд╣ рдПрдХ рд╡реЗрдм рд╕реЗрд╡рд╛ рд╣реИред
@Controller @RequestMapping(value = "/ws", produces = MediaType.APPLICATION_JSON_VALUE) public class ContactWsController { @Autowired private ContactService contactService; }
рд╡рд░реНрдЧ рд╕реНрддрд░ рдкрд░
@RequestMapping(value="/ws")
рдПрдХ рд╕рд╛рдорд╛рдиреНрдп рдЙрдкрд╕рд░реНрдЧ рд╕реЗрдЯ рдХрд░рддрд╛ рд╣реИ, рдЬреЛ рд╡реНрдпрдХреНрддрд┐рдЧрдд рд╡рд┐рдзрд┐рдпреЛрдВ рдХреЗ рд╕рднреА URL рдореЗрдВ рд╕реНрд╡рдЪрд╛рд▓рд┐рдд рд░реВрдк рд╕реЗ рдЬреЛрдбрд╝рд╛ рдЬрд╛рдПрдЧрд╛ред
produces = MediaType.APPLICATION_JSON_VALUE
рдХрд╣рдирд╛ рд╣реИ рдХрд┐ рдбрд┐рдлрд╝реЙрд▓реНрдЯ рд░реВрдк рд╕реЗ рдЗрд╕ рдирд┐рдпрдВрддреНрд░рдХ рдХреЗ рд╕рднреА рддрд░реАрдХреЗ JSON рдХреЛ рд╡рд╛рдкрд╕ рдХрд░ рджреЗрдВрдЧреЗред рдпрджрд┐ рдЖрд╡рд╢реНрдпрдХ рд╣реЛ, рддреЛ рдПрдХ рд╡рд┐рд╢рд┐рд╖реНрдЯ рд╡рд┐рдзрд┐ рдЗрд╕ рдорд╛рди рдХреЛ рдУрд╡рд░рд░рд╛рдЗрдб рдХрд░ рд╕рдХрддреА рд╣реИред
рдкрд░рдВрдкрд░рд╛ рд╕реЗ, рд╣рдо рд╕рдВрдкрд░реНрдХреЛрдВ рдХреА рдПрдХ рд╕реВрдЪреА рдХреЗ рд╕рд╛рде рд╢реБрд░реВ рдХрд░рддреЗ рд╣реИрдВред рдкреБрд░рд╛рдиреА рд╡рд┐рдзрд┐ рдЗрд╕ рддрд░рд╣ рджрд┐рдЦрддреА рд╣реИ:
@RequestMapping("/index") public String listContacts(Map<String, Object> map) { map.put("contact", new Contact()); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return "contact"; }
рдкреНрд░рддреНрдпреЗрдХ рд╡рд┐рдзрд┐ рдХреЗ рд▓рд┐рдП рдЕрдкрдирд╛ рд╕реНрд╡рдпрдВ рдХрд╛
рдбреАрдЯреАрдУ рдирд╣реАрдВ рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдП, рд╣рдо рдЗрд╕ рд╕рдордЭреМрддреЗ рдХреЛ рд╕реНрд╡реАрдХрд╛рд░ рдХрд░рддреЗ рд╣реИрдВ рдХрд┐ рдореИрдк рд▓реМрдЯреЗ JSON рдХреА рд╕рдВрд░рдЪрдирд╛ рд╣реЛрдЧреАред рд╣рдо рд╡рд┐рдзрд┐ рдкреИрд░рд╛рдореАрдЯрд░ рдХреЛ рд╣рдЯрд╛ рджреЗрддреЗ рд╣реИрдВ, рд╣рдореЗрдВ рдЗрд╕рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдирд╣реАрдВ рд╣реИ, рд░рд┐рдЯрд░реНрди рдорд╛рди рдХреЗ рдкреНрд░рдХрд╛рд░ рдХреЛ рдореИрдк рдореЗрдВ рдмрджрд▓реЗрдВ рдФрд░ рд░рд┐рдЯрд░реНрди рдорд╛рди рдкрд░
@ResponseBody
рдПрдиреЛрдЯреЗрд╢рди рдЬреЛрдбрд╝реЗрдВред рд╡рд╣ рд╕реНрдкреНрд░рд┐рдВрдЧ рдХреЛ рдмрддрд╛рдПрдЧреА рдХрд┐ рд╣рдо рдЪрд╛рд╣рддреЗ рд╣реИрдВ рдХрд┐ рдпрд╣ рдорд╛рди JSON рдореЗрдВ рдХреНрд░рдордмрджреНрдз рд╣реЛ рдФрд░ рдкреНрд░рддрд┐рдХреНрд░рд┐рдпрд╛ рдирд┐рдХрд╛рдп рдХреЛ рд▓рд┐рдЦрд╛ рдЬрд╛рдПред рдирдИ рд╡рд┐рдзрд┐ рдЗрд╕ рддрд░рд╣ рджрд┐рдЦрддреА рд╣реИ:
@RequestMapping(value = "/index") @ResponseBody public Map<String, Object> listContacts() { Map<String, Object> map = new HashMap<String, Object>(); map.put("contact", new Contact()); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; }
рдорд╛рдорд▓реЗ рдХреЛ рдПрдХ рддрд░рдл рди рд░рдЦрдиреЗ рдФрд░ рдЬрд▓реНрджреА рд╕реЗ рд╣рдорд╛рд░реЗ рдкреНрд░рдпрд╛рд╕реЛрдВ рдХрд╛ рдлрд▓ рджреЗрдЦрдиреЗ рдХреЗ рд▓рд┐рдП, рд╣рдо рдЕрдкрдиреА рдпреЛрдЬрдирд╛ рд╕реЗ рдереЛрдбрд╝рд╛ рдкреАрдЫреЗ рд╣рдЯреЗрдВрдЧреЗ рдФрд░ рддреБрд░рдВрдд рдЗрд╕ рдкрджреНрдзрддрд┐ рдХрд╛ рдкрд░реАрдХреНрд╖рдг рдХрд░рдиреЗ рдХрд╛ рдкреНрд░рдпрд╛рд╕ рдХрд░реЗрдВрдЧреЗред
реи.рез рдкреНрд░рдердо рдкрд░реАрдХреНрд╖рдг рд╡рд┐рдзрд┐
рд▓реЗрдХрд┐рди рдпрд╣ рд╕рдордЭрдирд╛ рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╣реИ рдХрд┐ рд╣рдореЗрдВ рдкрд░реАрдХреНрд╖рдг рдореЗрдВ рдХрд┐рд╕ рддрд░рд╣ рдХреЗ рдкрд░рд┐рдгрд╛рдореЛрдВ рдХреА рдЙрдореНрдореАрдж рдХрд░рдиреА рдЪрд╛рд╣рд┐рдПред рд╣рдо рдПрдХ рдирдпрд╛ рд╡рд░реНрдЧ
MockMvcWsTest.groovy
, рдЗрд╕рдореЗрдВ рдХреЙрдкреА рдХрд░реЗрдВ рд╕рднреА MockMvc рд╕реНрдерд╛рдкрд┐рдд рдХрд░рдиреЗ рд╕реЗ рд╕рдВрдмрдВрдзрд┐рдд рдореБрдЦреНрдп рд╕рд╛рдордЧреНрд░реАред рдФрд░ рдкреБрд░рд╛рдиреЗ рдкрд░реАрдХреНрд╖рдг рдХреА рдкреНрд░рддрд┐рд▓рд┐рдкрд┐ рдмрдирд╛рдПрдБ:
@Test public void index_user1() { mockMvc.perform(MockMvcRequestBuilders.get("/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andExpect(MockMvcResultMatchers.view().name("contact")) .andExpect(MockMvcResultMatchers.model().attributeExists("contact")) .andExpect(MockMvcResultMatchers.model().attributeExists("contactList")) .andExpect(MockMvcResultMatchers.model().attributeExists("contactTypeList")) }
рд╕рднреА рдбреЗрдЯрд╛ рдореЙрдбрд▓ рдореЗрдВ рдЖрддрд╛ рд╣реИред рд▓реЗрдХрд┐рди рд╡реЗрдм рд╕реЗрд╡рд╛ рдореЗрдВ рд╣рдо рдЙрдиреНрд╣реЗрдВ рдЕрдиреБрд░реЛрдз рдирд┐рдХрд╛рдп рдореЗрдВ рдЕрдЧреНрд░реЗрд╖рд┐рдд рдХрд░рдиреЗ рдЬрд╛ рд░рд╣реЗ рдереЗред рд╢рд╛рдпрдж рд╡рд╣рд╛рдБ рдпрд╣ рдЙрдирдХреЗ рд▓рд┐рдП рджреЗрдЦрдиреЗ рд▓рд╛рдпрдХ рд╣реИ? рдЖрдЗрдпреЗ рдЗрд╕реЗ рдЖрдЬрдорд╛рддреЗ рд╣реИрдВред
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andReturn()
рд╣рдо рд╢реБрд░реВ рдХрд░рддреЗ рд╣реИрдВред рдкрд░реАрдХреНрд╖рдг рдкреВрд░рд╛ рд╣реЛ рдЧрдпрд╛, рд▓реЗрдХрд┐рди рдХрдВрд╕реЛрд▓ рдореЗрдВ рдХреБрдЫ рднреА рдирд╣реАрдВ рд╣реИред рд╣рдореНрдоред рд╣рдореЗрдВ рдПрдХ рд╕реМрджрд╛ рдХрд░рдирд╛ рд╣реЛрдЧрд╛, рдирд┐рдпрдВрддреНрд░рдХ рдореЗрдВ рдПрдХ рдмреНрд░реЗрдХрдкреЙрдЗрдВрдЯ рдбрд╛рд▓рдирд╛ рд╣реЛрдЧрд╛, рдЗрд╕реЗ рдЪрд▓рд╛рдирд╛ рд╣реЛрдЧрд╛ред рд╣рд╛рдБ, рдпрд╣ рдирд┐рдпрдВрддреНрд░рдХ рдореЗрдВ рдЬрд╛рддрд╛ рд╣реИред рд▓реЗрдХрд┐рди рдХреБрдЫ рдЙрдкрдпреЛрдЧреА рд╡рд╛рдкрд╕ рдирд╣реАрдВ рдЖрддрд╛ рд╣реИред рд╣рдо рдкрд░рд┐рдгрд╛рдо рдореВрд▓реНрдп рдХреЛ рджреЗрдЦрддреЗ рд╣реИрдВ - рдпрд╣ рд╣реИ,
HttpMediaTypeNotAcceptableException
рдореЗрдВ рдПрдХ
HttpMediaTypeNotAcceptableException
, рдФрд░
mockResponse.status
= 406 рд╣реИред рдирд┐рджрд╛рди рдЕрднреА рднреА рдПрдХ рдирд┐рд░рд╛рд╢рд╛рдЬрдирдХ рд╣реИ рдЬреЛ рд╕реНрд╡реАрдХрд╛рд░реНрдп рдкреНрд░рддрд┐рдирд┐рдзрд┐рддреНрд╡ рдирд╣реАрдВ рдкрд╛ рд╕рдХрддрд╛ рд╣реИ тАЭред
рд▓реЗрдХрд┐рди рджреВрд╕рд░реА рддрд░рдл, рд╣рдордиреЗ рдореИрдк рдХреЛ JSON рдореЗрдВ рдмрджрд▓рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд╣рд╛, рдФрд░ рд╣рдордиреЗ рдпрд╣ рдирд╣реАрдВ рдХрд╣рд╛ рдХрд┐ рдЗрд╕реЗ рдХреМрди рдФрд░ рдХреИрд╕реЗ рдХрд░рдирд╛ рд╣реИред рд╡рд╕рдВрдд, рдЬрд╝рд╛рд╣рд┐рд░ рд╣реИ, рдмрд╣реБрдд рдХреБрдЫ рдХрд░ рд╕рдХрддрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рд╕рднреА рдирд╣реАрдВред
рдкреНрд░рд▓реЗрдЦрди рдХрд╛ рдЕрдзреНрдпрдпрди рд╣рдореЗрдВ рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдкрд░рд┐рдгрд╛рдо рджреЗрддрд╛ рд╣реИ:
pom.xml
, JSON рд▓рд╛рдЗрдмреНрд░реЗрд░реА рдореЗрдВ рдирд┐рд░реНрднрд░рддрд╛ рдЬреЛрдбрд╝реЗрдВред
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.3</version> </dependency>
- рдореЗрдВ xml рд╡рд┐рдиреНрдпрд╛рд╕ рдкрд░рд┐рд╡рд░реНрддрди
annotation-driven
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> <property name="favorParameter" value="true" /> <property name="mediaTypes"> <value> json=application/json </value> </property> </bean>
( servlet-context.xml
ред servlet-context.xml
рджреЗрдЦреЗрдВ)ред рдорд╣рддреНрд╡рдкреВрд░реНрдг - 3.2 рдХреЗ рд▓рд┐рдП spring-mvc-3.2.xsd
рдпреЛрдЬрдирд╛рдПрдВ рдмрджрд▓реЗрдВ (рдЬреИрд╕реЗ spring-mvc-3.2.xsd
)
- рдФрд░ рдкрд░реАрдХреНрд╖рдг рдХреЗ рд▓рд┐рдП рдЬрд╛рд╡рд╛ рд╡рд┐рдиреНрдпрд╛рд╕ рдореЗрдВ - рд╕рд┐рд░реНрдл рдПрдХ рдПрдиреЛрдЯреЗрд╢рди
@EnableWebMvc
рдареАрдХ рд╣реИ, рджреЛ рд╡рд┐рдиреНрдпрд╛рд╕реЛрдВ рдХреА рдЙрдкрд╕реНрдерд┐рддрд┐ рдирд┐рд╢реНрдЪрд┐рдд рд░реВрдк рд╕реЗ рдЕрд╕реБрд╡рд┐рдзрд╛ рдХрд╛ рдХрд╛рд░рдг рдмрдирдиреЗ рд▓рдЧреА рд╣реИ, рд▓реЗрдХрд┐рди рдЕрднреА рдХреЗ рд▓рд┐рдП рд╣рдо рдЗрд╕рд╕реЗ рд╡рд┐рдЪрд▓рд┐рдд рдирд╣реАрдВ рд╣реЛрдВрдЧреЗред рд╣рдо рдкрд░реАрдХреНрд╖рдг рд╢реБрд░реВ рдХрд░рддреЗ рд╣реИрдВ, рд╣рдо рд╡рд╛рдВрдЫрд┐рдд рд░реЗрдЦрд╛ рджреЗрдЦрддреЗ рд╣реИрдВред рд▓реЗрдХрд┐рди ... рд╕рд┐рд░рд┐рд▓рд┐рдХ рдХреЗ рдмрдЬрд╛рдп krakozyablyред
{"contactTypeList":[{"id":1,"code":"family","name":"├Р┬б├Р┬╡├Р┬╝├С┼Т├С ","defaulttype":false,"contacts":null},{"id":2,"code":"job","name":"├Р ├Р┬░├Р┬▒├Р┬╛├СтАЪ├Р┬░","defaulttype":false,"contacts":null},{"id":3,"code":"stuff","name":"├РтАФ├Р┬╜├Р┬░├Р┬║├Р┬╛├Р┬╝├СтА╣├Р┬╡","defaulttype":true,"contacts":null}],"contactList":[],"contact":{"id":null,"firstname":null,"lastname":null,"email":null,"telephone":null,"contacttype":null}}
рдлрд┐рд░ рд╕реЗ рдЧреВрдЧрд▓, рдзреБрдЖрдВ рдореИрдиреБрдЕрд▓ рдФрд░ рд╕реНрд░реЛрдд рдХреЛрдб рдФрд░ рдЗрд╕ рд╕рдорд╛рдзрд╛рди рдХреЛ рдЦреЛрдЬрдиреЗ рдХреЗ рд▓рд┐рдП: charset рдХреЛ @ RequestMapping.produces (рд╡рд░реНрдЧ рд╕реНрддрд░ рдкрд░ рдПрдХ) рдореЗрдВ рдЬреЛрдбрд╝реЗрдВред
@RequestMapping(value = "/ws", produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8" )
рдпрд╣ рдПрдХ рдлрд╡реНрд╡рд╛рд░реЗ рдХреА рддрд░рд╣ рдирд╣реАрдВ рджрд┐рдЦрддрд╛ рд╣реИ, рдЬреЛ рд▓реЛрдЧ "рдЕрдзрд┐рдХ рдЕрд▓рдЧ" рд╡рд┐рдХрд▓реНрдк рдХреА рддрд▓рд╛рд╢ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ, рд╣рдо рд╡рд╣рд╛рдВ рд░реБрдХ рдЬрд╛рдПрдВрдЧреЗред
рдХреБрдЫ рдФрд░ рдЯрд┐рдкреНрдкрдгрд┐рдпрд╛рдВ
рдЯрд┐рдкреНрдкрдгреА 1 ред рдкрд░реАрдХреНрд╖рдг рдХреЗ рдЕрдВрджрд░ рдХреНрдпрд╛ рдЪрд▓ рд░рд╣рд╛ рд╣реИ, рдпрд╣ рд╕рдордЭрдирд╛
.andDo(MockMvcResultHandlers.print())
рдирд┐рд░реНрдорд╛рдг рджреНрд╡рд╛рд░рд╛ рд╕реБрдЧрдо рд╣реЛрдЧрд╛
.andDo(MockMvcResultHandlers.print())
рдпрд╣ рд▓реЙрдЧ рдореЗрдВ рд╡рд┐рд╕реНрддреГрдд рдЬрд╛рдирдХрд╛рд░реА рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд░рддрд╛ рд╣реИред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рд╣рдорд╛рд░реЗ рдЕрдиреБрд░реЛрдз рдХреЗ рд▓рд┐рдП, рд▓реЙрдЧ рдЗрд╕ рддрд░рд╣ рджрд┐рдЦреЗрдЧрд╛ред
MockHttpServletRequest: HTTP Method = GET Request URI = /ws/index Parameters = {} Headers = {} Handler: Type = net.schastny.contactmanager.web.ContactWsController Method = public java.util.Map<java.lang.String, java.lang.Object> net.schastny.contactmanager.web.ContactWsController.listContacts() Async: Was async started = false Async result = null Resolved Exception: Type = null ModelAndView: View name = null View = null Model = null FlashMap: MockHttpServletResponse: Status = 200 Error message = null Headers = {Content-Type=[application/json;charset=UTF-8]} Content type = application/json;charset=UTF-8 Body = {"contactTypeList":[{"id":1,"code":"family","name":"","defaulttype":false,"contacts":null},{"id":2,"code":"job","name":"","defaulttype":false,"contacts":null},{"id":3,"code":"stuff","name":"","defaulttype":true,"contacts":null}],"contactList":[],"contact":{"id":null,"firstname":null,"lastname":null,"email":null,"telephone":null,"contacttype":null}} Forwarded URL = null Redirected URL = null Cookies = []
рдмрд╣реБрдд рдмреЗрд╣рддрд░ рд╣реИ рдирд╛?
рдЯрд┐рдкреНрдкрдгреА 2 ред рдпрд╣ рджреЗрдЦрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ рдХрд┐ рдХреЙрдиреНрдЯреЗрдХреНрдЯ рдЯрд╛рдЗрдк рдХреНрд▓рд╛рд╕ JSON рдореЗрдВ рд╕рднреА рд╡рд┐рд╢реЗрд╖рддрд╛рдУрдВ рдХреЗ рд╕рд╛рде рдбрд┐рдлрд╝реЙрд▓реНрдЯ рд░реВрдк рд╕реЗ рдХреНрд░рдордмрджреНрдз рд╣реИред рд▓реЗрдХрд┐рди
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
рдХреА рдХреНрд░рдордмрджреНрдзрддрд╛
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
рд╡рд┐рд╢реЗрд╖рддрд╛
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub
List contacts = null , - No Session, . - " " @JsonIgnore
@JsonIgnore @OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REFRESH, CascadeType.MERGE], mappedBy = "contacttype") List<Contact> contacts = null
, . .
@Test public void index_user1() { def result = mockMvc.perform(MockMvcRequestBuilders.get("/ws/index") .with(SecurityRequestPostProcessors.userDetailsService(USER1))) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(result.response.contentAsString, Map.class); assert !map.contactList assert map.contactTypeList.size() == 3 }
index_admin()
index_na()
.
2.2
.
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { contactService.addContact(contact); return "redirect:/index"; }
- :
Map @ResponseBody
, JSON- ( @RequestBody String json
) JSON , , Map, , index
:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE) @ResponseBody public Map<String, Object> addContactWs(@RequestBody String json) { Contact contact = null; try { contact = new ObjectMapper().readValue(json, Contact.class); contactService.addContact(contact); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", " "); // - - map.put("contact", contact); map.put("contactList", contactService.listContact()); map.put("contactTypeList", contactService.listContactType()); return map; } catch (IOException e) { Map<String, Object> map = new HashMap<String, Object>(); map.put("status", ""); map.put("message", e.getMessage()); return map; } }
@RequestMapping
consumes = MediaType.TEXT_PLAIN_VALUE
, "". - status, . . JSON. Jackson ObjectMapper(), Groovy JSON-builder, Groovy 1.8. JSON , contentType . :
@Test public void add_user1() { def contacts = contactService.listContact() assert !contacts def contactTypes = contactService.listContactType() assert contactTypes // Groovy JSON-builder def jsonBuilder = new JsonBuilder() jsonBuilder { firstname '' lastname '' email 'ivan.ivanov@gmail.com' telephone '555-1234' contacttype ( id : contactTypes[0].id ) } String json = jsonBuilder.toString() MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/ws/add") .contentType(MediaType.TEXT_PLAIN) .content(json) .with(SecurityRequestPostProcessors.userDetailsService(user)) MvcResult ret = mockMvc.perform(requestBuilder) .andExpect(MockMvcResultMatchers.content().contentType("${MediaType.APPLICATION_JSON_VALUE};charset=UTF-8")) .andDo(MockMvcResultHandlers.print()) .andReturn() def map = new ObjectMapper().readValue(ret.response.contentAsString, Map.class); assert map.status == ' ' assert map.contactList assert map.contactTypeList contacts = contactService.listContact() assert contacts assert contacts[0].id // Contact contact = new Contact(map.contact) assert contact.class == Contact.class assert contact.id == contacts[0].id assert contact.firstname == '' assert contact.lastname== '' assert contact.email == 'ivan.ivanov@gmail.com' assert contact.telephone == '555-1234' assert contact.contacttype.id == contactTypes[0].id contactService.removeContact(contacts[0].id) }
user1, . JSON, 401 .
@Test public void add_na() { ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/ws/add") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.TEXT_PLAIN) .characterEncoding("UTF-8") .content('{"J":5,"0":"N"}') //.with(SecurityRequestPostProcessors.userDetailsService(ADMIN)) ) result.andExpect(MockMvcResultMatchers.status().isUnauthorized()) }
, REST- . - . Basic Authentication , . HTTPS - .
.
GitHub