Java SpringREST-手动解析@RequestBody中的JSON属性

Java SpringREST-手动解析@RequestBody中的JSON属性,java,json,spring,rest,jackson,Java,Json,Spring,Rest,Jackson,我有实体类: @Entity @Table(name="person") public class Person implements Serializable { @Id @Column(unique=true) private int id; private String title; // getter, setter, constructor,... } 在控制器中: @RequestMapping(value="/get/{id}", metho

我有实体类:

@Entity
@Table(name="person")
public class Person implements Serializable {

    @Id @Column(unique=true)
    private int id;

    private String title;

    // getter, setter, constructor,...
}
在控制器中:

@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
    public @ResponseBody Person getPerson(@PathVariable int id) {
        return personManager.findById(id);
    }

@RequestMapping(value="/add", method=RequestMethod.POST)
    public @ResponseBody void addPerson(@RequestBody Person person) {      
        String log = parse_json_from_input("log"); // How can I do it?
        // do something with log
        personManager.save(person);           
    }
我想用JSON发送额外的参数并对其进行解析。若我执行下面的命令,我会得到Person实体,这是正常的。但是我需要在
addPerson
方法中获取
log
属性,以用于其他用途

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" \
 -d '{"title":"abc","log":"message..."}' http://localhost:8080/test/add

如何解析它?

使用SpringRestTempalge

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Person person = get some where
result = restTemplate.postForObject("http://localhost:8080/test/add", Person, Person.class);
使用HttpURLConnection

public class Test {
    public static void main(String[] args) {
        try {

            URL url = new URL("http://localhost:8080/test/add");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            OutputStream outputStream = conn.getOutputStream();
            String requestMessage = get your  preson json string
            outputStream.write(requestMessage.getBytes());
            outputStream.flush();

            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String output;
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

希望您已经有了Jackson JSON依赖项

你可以在这里找到它:

我将尝试以下代码:

ObjectMapper mapper = new ObjectMapper();

@RequestMapping(value="/add", method=RequestMethod.POST)
public @ResponseBody void addPerson(@RequestBody String json) {     

    ObjectNode node = mapper.readValue(json, ObjectNode.class);

    if (node.get("log") != null) {
        String log = node.get("log").textValue();
        // do something with log
        node.remove("log"); // !important
    }

    Person person = mapper.convertValue(node, Person.class);
    personManager.save(person);           
}
这就应该做到了

确保检查并删除Person POJO中没有的任何“额外”字段