在Micronaut Controller中,使用JSON对象解析发布请求。我希望它不包括引号,但在数据库插入中将其引号。
像这样发布:
curl -X POST --header "Content-Type: application/json" -d '{"bookid":3,"name":"C++"}' http://localhost:8880/book/save
保存如下:
String bookid=JSON?.bookid
String name=JSON?.name
def b =bookService.save(bookid,name
在数据库中存储如下:
+--------+-------+
| bookid | name |
+--------+-------+
| 3 | "C++" |
+--------+-------+
我希望图书名称仅为C++
谢谢 SR
答案 0 :(得分:2)
我知道我来晚了一点,但我一直在寻找解决方案很长时间,但是没有找到任何东西。经过大量的努力,我发现发送带有“ @BODY”的杰克逊“对象”没有帮助,当我将类型更改为“字符串”时,它对我有用。
def save(@Body String JSON){
def result = [:]
ObjectMapper objectMapper = new ObjectMapper();
//convert json string to object
def obj = objectMapper.readValue(JSON, Course.class);
println(obj)
Course course = new Course(name: obj?.name, pre: obj?.pre,
regno: obj?.regno, enrolled: obj?.enrolled)
course.validate()
if(course.hasErrors()){
println("Error: "+course.errors)
result.put("Error is: ",course.errors)
return result;
}
course.save(flush:true,failOnError: true)
result.put("Message","Successfully Created")
result.put("Result",course)
return HttpResponse.created(result)
}
将其传递给ObjectMapper,然后将其从JSON字符串转换为Java Object对我来说很有效。
我传递的{
"name" : "Data Structures",
"pre" : "Computer Programming",
"regno" : "249",
"enrolled" : "90"
}
这是数据库更改前后的数据存储:
+----+---------+------------------------+-------------------------------+----+
| id | version | name | pre | regno |enrolled |
+----+---------+------------------------+-------------------------------+----+
| 1 | 0 | "Computer Programming" | "Introduction to Programming"| "233"|"26"|
| 2 | 0 | Data Structures | Computer Programming | 249 | 90 |
+----+---------+------------------------+-------------------------------+----+
希望答案能帮助正在寻找上述解决方案替代方案的人。
答案 1 :(得分:0)
您没有提供有关项目的足够信息以了解发生了什么,但是https://github.com/jeffbrown/sfgroupsjsonbinding/tree/master上的项目演示了内置绑定内容的工作方式。在此处查看README.md文件。
package sfgroupsjsonbinding
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Post
@Controller('/book')
class BookController {
private PersonService personService
BookController(PersonService personService) {
this.personService = personService
}
@Get('/')
List<Person> list() {
personService.list()
}
@Post('/')
Person save(Person person) {
personService.save person
}
@Get('/{id}')
Person get(long id) {
personService.get id
}
}
与应用交互
$ curl -H "Content-Type: application/json" -d '{"name":"Jeff"}' http://localhost:8080/book
{"name":"Jeff","id":1}
$
$ curl -H "Content-Type: application/json" -d '{"name":"Jake"}' http://localhost:8080/book
{"name":"Jake","id":2}
$
$ curl -H "Content-Type: application/json" -d '{"name":"Zack"}' http://localhost:8080/book
{"name":"Zack","id":3}
$
$ curl http://localhost:8080/book
[{"name":"Jeff","id":1},{"name":"Jake","id":2},{"name":"Zack","id":3}]
$
$ curl http://localhost:8080/book/1
{"name":"Jeff","id":1}
$
$ curl http://localhost:8080/book/2
{"name":"Jake","id":2}
$
$ curl http://localhost:8080/book/3
{"name":"Zack","id":3}
$
答案 2 :(得分:0)
看来我的命令类扩展了其他类正在引起一些问题,这意味着什么都不起作用。目前,它确实有效,并且实际上是Zaryab baloch上面建议的替代方案。
package gateway.command.controller;
import com.fasterxml.jackson.annotation.JsonProperty;
import gateway.command.event.commands.HotelSaveCommand;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.jackson.codec.JsonMediaTypeCodec;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
@Slf4j
@Controller("/")
public class GatewayController {
@Inject
protected MediaTypeCodecRegistry mediaTypeCodecRegistry;
/**
*
* @param topic
* @param eventType using the jsonProperty we actually extract eventType from the @Body string JSON String
* The 3rd input is actual form. we post /hotel and json content there isn't actually 3 parameters
* provided
* @param
* @return
*/
@Post(uri = "/{topic}", consumes = MediaType.APPLICATION_JSON)
public HttpResponse process(String topic, @JsonProperty("eventType") String eventType, @Body String formInput) {
JsonMediaTypeCodec mediaTypeCodec = (JsonMediaTypeCodec) mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE)
.orElseThrow(() -> new IllegalStateException("No JSON codec found"));
HotelSaveCommand command = mediaTypeCodec.decode(HotelSaveCommand.class,formInput);
if (command!=null) {
System.out.println(command +" "+ command.getName());
}
//eventPublisher.publish(topic,);
return HttpResponse.accepted();
}
}