json - Spring 3.2 Jackson 2.2 REST API

标签 json spring api rest

我从 REST 技术开始,选择 Spring 3.2 和 Jackson 2.2 。我有一个小问题。我创建了 REST API,它看起来像这样:

@Controller
public class WorkersController {

@Autowired
public DatabaseService dbService;

@ResponseBody
@RequestMapping(value="/workers", method = RequestMethod.GET, produces = "application/json")
public ArrayList<Worker> getAllWorkersFromDatabase() {
}

@ResponseBody
@RequestMapping(value="/workers/new", method = RequestMethod.POST, produces = "application/json", consumes="application/json")
public String saveWorker(@RequestBody final WorkerDTO workerDto) {
}

@ResponseBody
@RequestMapping(value="/workers/{workerid}", method = RequestMethod.GET, produces = "application/json")
public Worker getWOrkerByDatabaseId(@PathVariable Integer workerid) {
}

@ResponseBody
@RequestMapping(value="/workers/{workerid}/edit", method = RequestMethod.PUT, produces = "application/json")
public String editWorker(@PathVariable Integer workerid, @RequestBody Worker worker) {
}
}

当我进行 HTTP GET 时一切正常,但 POST 时出现问题。当我调用 saveWorker() 方法时,我得到:

The server refused this request because the request entity is in a format not  supported by the requested resource for the requested method

我导入了所需的库:

 <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>

我认为主要问题出在配置文件中,@RequestBody 无法将 JSON 映射到 DTO。这是我的配置:

@Configuration
@ComponentScan(basePackages = "org.schedule.service")
@EnableWebMvc
@Import(DatabaseSpringConfig.class)
public class ServiceSpringConfig extends WebMvcConfigurationSupport{

@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    List<MediaType> jsonTypes = new ArrayList<>(jsonConverter.getSupportedMediaTypes());
    jsonTypes.add(MediaType.TEXT_PLAIN);
    jsonTypes.add(MediaType.APPLICATION_JSON);
    jsonConverter.setSupportedMediaTypes(jsonTypes);
    converters.add(jsonConverter);
}

}

我的 DTO:

public class WorkerDTO implements Serializable {

private static final long serialVersionUID = 1L;

public String name;
public String surname;

public WorkerDTO() {
}
}

Json:

{
"name": "asdssss",
"surname": "asdssssss"
}

和http调用:

localhost:8080/Schedule-service/workers/new?Content-type=application/json

感谢大家的回复。

最佳答案

请求

localhost:8080/Schedule-service/workers/new?Content-type=application/json

有一个请求参数,名称为Content-Type,值为application/json

HttpMessageConverter 类,特别是 MappingJackson2HttpMessageConverter,不查找请求参数,而是查找 header 。

您需要为您的请求指定一个 Content-Type header 。

关于json - Spring 3.2 Jackson 2.2 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162679/

相关文章:

django - 可以使用spotify API让每个人都听一首歌吗?

amazon-web-services - 如何将 "aws_apigatewayv2_route"与 "aws_apigatewayv2_integration"链接起来?

java - Jackson 自定义序列化程序,用于某些字段的自定义行为,同时对其余字段具有默认行为

javascript - 如何使用 Ajax 和 JSON 获取天气信息并将其放入 HTML 段落中?

c - 使用 C 解析 JSON

java - Spring @Transactional 隔离传播

Java框架创建简单的网站?

javascript - 使用 JSON 按数据属性选择 div

java - 我们需要 Spring Beans 中的静态字段吗?

api - 如何将太大的 Kaggle 数据集的一个选定文件从 Kaggle 加载到 Colab 中