I am currently struggling with an error that appears in my Chrome developer tools console
GET http://localhost:8080/movie_db/search/movie?movieTitle=Machete 415 (Unsupported Media Type)
when I try to connect to my REST service with angular JS. Since I had this issue before when I tried to access the REST service via a Simple REST plugin in Chrome, I assume that most likely a missing Content-Type header parameter is the reason for that (should be Content-Type: 'application/json').
Angular JS website says, that one could set header parameters as follows: $httpProvider.defaults.headers.get['My-Header']='value'. Regretfully this didn't have any impact and the problem still exists.
Thus I'd like to know how I can manipulate the header parameters with angular JS that are sent over to my REST service.
Right at the moment, my code looks like this:
function SearchMovieController($scope, $resource) {
$scope.SearchMovie = $resource('http://localhost\\:8080/movie_db/search/:action',
{action: 'movie', movieTitle: 'Machete'},
{get: {method: 'JSONP', headers: [{'Content-Type': 'application/json'}, {'Accept' : 'application/json'}]}});
$scope.SearchMovie.get()
}
Server side looks as follows (I use Spring MVC):
CONTROLLER:
@Controller
@RequestMapping( "/search/movie" )
public class SearchController { // TODO rename?
private final TheMovieDBService theMovieDBService;
@Autowired
public SearchController( TheMovieDBService theMovieDBService ) {
this.theMovieDBService = theMovieDBService;
}
@ResponseBody
@RequestMapping( method = RequestMethod.GET, produces = "application/json", consumes = "application/json" )
public Map<String, Object> search( @RequestParam String movieTitle ) {
Map<String, Object> response = new HashMap<String, Object>();
try {
TheMovieDBSearchResult searchResult = theMovieDBService.searchMovie( movieTitle );
response.put( "result", searchResult );
response.put( "success", "true" );
} catch ( EncoderException e ) {
response.put( "success", "false" );
}
return response;
}
}
web.xml
<servlet>
<servlet-name>json</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.u6f6o.apps.movie_db.config.web.ServletConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>json</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<script>tag to the page rather than using AJAX.