33import cn .netbuffer .springboot .demo .User ;
44import lombok .extern .slf4j .Slf4j ;
55import org .springframework .beans .factory .annotation .Value ;
6+ import org .springframework .http .HttpStatus ;
67import org .springframework .http .MediaType ;
8+ import org .springframework .http .ResponseEntity ;
79import org .springframework .util .StopWatch ;
810import org .springframework .web .bind .annotation .*;
911import org .springframework .web .util .ContentCachingRequestWrapper ;
@@ -114,9 +116,9 @@ public Date date(Date date) {
114116 }
115117
116118 @ PostMapping (value = "user" , produces = {MediaType .APPLICATION_JSON_UTF8_VALUE })
117- public User user (@ RequestBody User user ,HttpServletRequest httpServletRequest ) {
119+ public User user (@ RequestBody User user , HttpServletRequest httpServletRequest ) {
118120 log .info ("receive user:{}" , user );
119- ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils .getNativeRequest (httpServletRequest , ContentCachingRequestWrapper .class );
121+ ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils .getNativeRequest (httpServletRequest , ContentCachingRequestWrapper .class );
120122 //重复读取http body
121123 log .debug ("http request body:{}" , new String (contentCachingRequestWrapper .getContentAsByteArray ()));
122124 return user ;
@@ -166,4 +168,17 @@ public String defaultValue(@RequestParam(defaultValue = "default") String value)
166168 return value ;
167169 }
168170
169- }
171+ @ GetMapping ("mediatype" )
172+ public ResponseEntity mediatype (String value , String type ) {
173+ MediaType mediaType = null ;
174+ if (type .equalsIgnoreCase ("text" )) {
175+ mediaType = MediaType .TEXT_PLAIN ;
176+ } else if (type .equalsIgnoreCase ("json" )) {
177+ mediaType = MediaType .APPLICATION_JSON ;
178+ } else if (type .equalsIgnoreCase ("xml" )) {
179+ mediaType = MediaType .TEXT_XML ;
180+ }
181+ return ResponseEntity .status (HttpStatus .OK ).contentType (mediaType ).body (value );
182+ }
183+
184+ }
0 commit comments