Reputation: 840
I am beginning with REST techniologies, and I choose Spring 3.2 and Jackson 2.2 . I have small question. I created REST API and it looks like this:
@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) {
}
}
When I make HTTP GET all is ok but I have problem with POST. When I am calling saveWorker() method I get:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
I imported required libraries:
<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>
I think the main problem is in configuration files and @RequestBody cant map JSON to DTO. It is my Configuration:
@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);
}
}
My 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"
}
And http call:
localhost:8080/Schedule-service/workers/new?Content-type=application/json
Thanks for all replies.
Upvotes: 2
Views: 927
Reputation: 279880
The request
localhost:8080/Schedule-service/workers/new?Content-type=application/json
has a request parameter with name Content-Type
and value application/json
.
HttpMessageConverter
classes, and MappingJackson2HttpMessageConverter
in particular, don't look for request parameters, they look for headers.
You need to specify a Content-Type
header for your request.
Upvotes: 4