- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 spring boot Controller 端点,如下所示。
@PutMapping("/manage/{id}")
public ResponseEntity<Boolean> manage(@PathVariable Long id, @RequestBody Type type) {
...
}
其中 Type 是一个枚举,如下所示。
public enum Type {
ONE,
TWO
}
问题 1:当我测试这个 Controller 时,我必须将内容作为 "ONE"
而不是 ONE
发送才能成功调用.即它适用于以下代码。
mvc.perform(put("/api/manage/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content("\"" + Type.ONE + '\"'))
.andExpect(status().isOk());
它不适用于
mvc.perform(put("/api/manage/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(Type.ONE.name()))
.andExpect(status().isOk());
问题 2:我无法从 Angular 服务调用此方法。
this.http.put<string>('/api/manage/' + id, type)
给我
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
当我将枚举添加到 Dto 并从客户端发送对象时,一切正常。但是由于一些业务需求,我想使用当前结构本身。即枚举作为 RequestBody
。
更新
我还尝试将 Controller 方法结构更改为
@PutMapping(value = "/manage/{id}", consumes = MediaType.TEXT_PLAIN_VALUE)
我收到以下错误。
Content type 'text/plain' not supported
最佳答案
这两个问题都源于尝试将 JSON 端点用作纯文本端点。
广告 1,ONE
是无效的 JSON(“ONE”
是有效的)
广告 2,当您只发布一个字符串时,它会作为 text/plain
发送,并且端点会提示。
可能将 consumes="text/plain"
添加到您的 @PutMapping
将解决问题,但坦率地说 - 我不确定字符串/枚举映射是否有效 - Spring Boot 大杂烩中的开箱即用。
关于java - 以 Enum 作为 RequestBody 问题的 Spring boot PutMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53011390/
我只是尝试使用 putMapping 创建索引,并且相当确定这是一个语法问题。弹性客户端适用于 .index 和 .search 命令,因此我认为它的设置不是问题。相反,它与我的 putmapping
在本文中,我们将讨论 Spring 4.3。引入了 @RequestMapping 的特定于 HTTP 方法的快捷方式变体。 Spring RequestMapping 新的 Shortcut Ann
我们有一个如下所示的 Controller : @RestController() public class TestController { @Autowired TestService t
我真的不明白这些文档。即使在阅读了一些SO问题和答案之后(例如: this one )。我有这个代码 let indexMapping = { 'foo': { name: { type:
我想将 Put 和 Post 映射请求应用到一个方法,如下所示。它确实适用于 PUT,但不适用于 POST 请求。我哪里错了? @RestController @RequestMapping("/PQ
我正在尝试使用 Spring RestController 从我的 Oracle 数据库更新一个实体。无论我在我的 Controller 方法上使用 PostMapping 还是 PutMapping
我正在尝试使用 Elasticsearch java api 动态创建映射。这很重要,因为我不想更改编译的代码来更改映射。 几乎所有示例都使用 XContentBuilder 来执行此操作,但我想使用
Springboot版本2.1.8.RELEASE controller @PutMapping("/sample/{id}") public Sample update(@PathV
我有一个 spring boot Controller 端点,如下所示。 @PutMapping("/manage/{id}") public ResponseEntity manage(@PathV
我的本地 ES 1.3.4 实例和 JDBC For MySql 1.3.4.4 上有一条 River 这条河流运行良好,正在 ES 中导入数据。我面临的问题是我的字段之一是文本字段并且其中有空格
我是一名优秀的程序员,十分优秀!