0
votes

EDIT: still currently getting this as an error: 2019-08-08 15:12:50.876 WARN 3164 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]

I'm not really sure why it's saying Content type ' ' not supported... Also it works from postman but doesn't show xml, just shows json, even though I have selected application/xml for the header type

I'm trying to get my basic spring rest service to respond with XML. I've annotated all the elements and put headers that are supposed to accept XML. For some reason my client still gets the error "unsupported media type".

I tried putting @ResponseBody next to the methods in EmployeeService, but it ended up giving me a different error which was: [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]

Employee.java

public class Employee implements Serializable {

    @XmlAttribute
    private String id;

    @XmlElement
    private String name;

    @XmlElement
    private String description;
    //private List<Team> teams;


    public Employee() {
        super();
    }

    public Employee(String id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
        //this.teams = teams;

    }


    @XmlAttribute
    public String getId() {
        return id;
    }
    @XmlAttribute
    public void setId(String id) {
        this.id = id;
    }
    @XmlElement
    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement
    public String getDescription() {
        return description;
    }

//  public List<Team> getTeam() {
//      return teams;
//  }
    @XmlElement
    public void setDescription(String description) {
        this.description = description;
    }





    @Override
    public String toString() {
        return String.format(
                "employee [id=%s, name=%s, description=%s]", id,
                name, description);
    }
}

EmployeeController.java

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping(path = "/employees", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public @ResponseBody HashMap<String, Employee> retrieveEmployees() {
        return employeeService.retrieveAllEmployees();
    }

    @GetMapping(path = "/employees/{employeeId}",produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    public @ResponseBody Employee retrievebyId(@PathVariable String employeeId) {
        return employeeService.retrieveEmployee(employeeId);
    }

    @PostMapping(path="/employees")
    public ResponseEntity<Void> registeremployee(@RequestBody Employee newemployee) {

        Employee employee = employeeService.addEmployee(newemployee.getId(),newemployee.getName(), newemployee.getDescription());

        if (employee == null)
            return ResponseEntity.noContent().build();

        URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(
                "/{id}").buildAndExpand(employee.getId()).toUri();

        return ResponseEntity.created(location).build();
    }

}

EmployeeService.java

@Component
public class EmployeeService {


    static HashMap<String, Employee> employees = new HashMap<>();





    static {
        //Initialize Data

        Team team1 = new Team("t1", "Java team", "Java Dev Team");

        Employee Joe = new Employee("employee1", "Joe Smith","Human Resources");

        Employee Bob = new Employee("employee2", "Bob Jones",
                "Developer");

        employees.put("employee1", Joe);
        employees.put("employee2", Bob);
    }

    public HashMap<String, Employee> retrieveAllEmployees() {
        return employees;
    }

    public Employee retrieveEmployee(String employeeId) {
        return employees.get(employeeId);
    }
    //private SecureRandom random = new SecureRandom();

    public Employee addEmployee(String id, String name, String description) {

        //String randomId = new BigInteger(130, random).toString(32);
        Employee employee = new Employee(id, name, description);

        employees.put(id, employee);

        return employee;
    }
}

RestClient.java

public class RestClient {

     public static void getJsonEmployee(String id) throws JSONException, IOException {

         String uri = "http://localhost:8080/employees/" + id;



         RestTemplate restTemplate = new RestTemplate();

         HttpHeaders headers = restTemplate.headForHeaders(uri);


         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));


         String result = restTemplate.getForObject(uri, String.class);
         System.out.println(result);

         }




public static void postJsonEmployee(String id, String name, String description) {

    final String uri = "http://localhost:8080/employees/";


    Employee newemp = new Employee(id, name, description);


    RestTemplate restTemplate = new RestTemplate();


    HttpHeaders httpHeaders = restTemplate.headForHeaders(uri);


    httpHeaders.setContentType(MediaType.APPLICATION_XML);


    Employee result = restTemplate.postForObject( uri, newemp, Employee.class);


    httpHeaders.setContentType(MediaType.APPLICATION_XML);


     }

    public static void main(String[] args) throws IOException, JSONException {

     System.out.println("GET or POST?");
     BufferedReader getpost = new BufferedReader(new InputStreamReader(System.in));
     String selection = getpost.readLine();

     switch(selection) {

     case "GET":

     System.out.println("Type in the employee's ID");
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String employeeid = reader.readLine();
     getJsonEmployee(employeeid);
     break;

     case "POST":

         System.out.println("Type in the employee's ID");
         Scanner scan = new Scanner(System.in);
         String newid = scan.nextLine();
         System.out.println("Type in the employee's name");
         String newname = scan.nextLine();
         System.out.println("Type in the employee's description");
         String newdesc = scan.nextLine();
         postJsonEmployee(newid, newname, newdesc);
         break;

     }
} 

}

10:00:37.986 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP HEAD http://localhost:8080/employees/

10:00:38.293 [main] DEBUG org.springframework.web.client.RestTemplate - Response 415 UNSUPPORTED_MEDIA_TYPE Exception in thread "main"

org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 null at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:95) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) at org.springframework.web.client.RestTemplate.headForHeaders(RestTemplate.java:362) at com.client.clientmodel.RestClient.getJsonEmployee(RestClient.java:52) at com.client.clientmodel.RestClient.main(RestClient.java:104)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot</groupId>
    <artifactId>employee-model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>employee-model</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        </dependency>
        <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
            <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>



</project>
3
Where you are getting this error in postEmployee method or getEmployee method ?Sambit
@Sambit this is when I run RestClient.java and the getJSONemployee method executescccc
Did you try in Postman client with this url like this localhost:8080/employees/someId ?Sambit
@sambit yes it works but it comes back as JSON, i need it to come back in XMLcccc
Ok, Great, then in the same PostMan client , set Header as Accept as key and application/xml as value. After that you check, once you receive, let me know.Sambit

3 Answers

0
votes

The 405 indicates your client is sending a bad request. You are issuing an HTTP HEAD request in your client. Try just "newing" HttpHeaders.

Also can't set headers on a request after sending the request.

0
votes

Http status code series 4xx means a client-side problem. 415 means Unsupported media type. I am not a java guy but I faced exactly this issue before at ajax call. To solve this issue I add a headder which is Accept: "*/*" this solution is for ajax request. For java, I believe it will be

     public static void getJsonEmployee(String id) throws JSONException, IOException {

     String uri = "http://localhost:8080/employees/" + id;



     RestTemplate restTemplate = new RestTemplate();

     HttpHeaders headers = restTemplate.headForHeaders(uri);


     headers.add("Accept", "*/*");


     String result = restTemplate.getForObject(uri, String.class);
     System.out.println(result);

     }
0
votes

Can you try with the following code. I have modified only getJsonEmployee() method.

public static void getJsonEmployee(String id) throws JSONException, IOException {

     String uri = "http://localhost:8080/employees/" + id;

     RestTemplate restTemplate = new RestTemplate();

     HttpHeaders headers = new HttpHeaders();
     headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
     HttpEntity entity = new HttpEntity(headers)


    ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
     String result = response.getBody();
     System.out.println(result);

     }

Besides, I see that your Employee class to generate XML is not right, I provide below the proposed Employee class and there is a main method also so that you can run to test the generation of XML structure.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

@XmlRootElement
public class Employee implements Serializable {

  private String id;

  private String name;

  private String description;
  // private List<Team> teams;

  public Employee() {
    super();
  }

  public Employee(String id, String name, String description) {
    this.id = id;
    this.name = name;
    this.description = description;
    // this.teams = teams;

  }

  @XmlAttribute
  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  @XmlElement
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @XmlElement
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @Override
  public String toString() {
    return String.format("employee [id=%s, name=%s, description=%s]", id, name, description);
  }

  public static void main(String[] args) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Employee emp = new Employee("id1", "name-1", "desc-1");
    marshaller.marshal(emp, System.out);
  }
}