Skip to content

Commit 4b44eac

Browse files
committed
test ResponseEntity
1 parent 91fda0a commit 4b44eac

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

help/http-requests.http

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ GET http://localhost:9100/param/defaultValue
2323
GET http://localhost:9100/sse/sendById?id=uitest&message=hello
2424

2525
### /validator/validateUser
26-
GET http://localhost:9100/validator/validateUser?name=netbuffer&age=32&mobile=13232233345&createTime=1672506061000
26+
GET http://localhost:9100/validator/validateUser?name=netbuffer&age=32&mobile=13232233345&createTime=1672506061000
27+
28+
### /param/mediatype
29+
GET http://localhost:9100/param/mediatype?value=<user><name>netbuffer</name></user>&type=xml
30+
31+
### /param/mediatype
32+
GET http://localhost:9100/param/mediatype?value=hello world&type=text

src/main/java/cn/netbuffer/springboot/demo/controller/ParamController.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import cn.netbuffer.springboot.demo.User;
44
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.http.HttpStatus;
67
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
79
import org.springframework.util.StopWatch;
810
import org.springframework.web.bind.annotation.*;
911
import 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

Comments
 (0)