我與例如工作從https://spring.io/guides/gs/rest-service/和我改變默認類的招呼我自己的類,當我打電話這在Web瀏覽器中http://localhost:8080/greeting我得到的答案: There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation
彈簧安置服務
我的控制器:
package rest;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import database.*;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
private DBAccess dbaccess= new DBAccess();
@RequestMapping("/greeting")
public Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Customer(1,"a","b");
}
}
和客戶類:
package database;
public class Customer {
private long id;
private String firstName, lastName;
public Customer(){};
public Customer(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
void setFirstName(String firstName){
this.firstName=firstName;
}
void setLastName(String lastName){
this.lastName=lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
以下Spring MVC Rest/JSON service我加了傑克遜映射器的依賴,但它不工作...
請幫我
根據星雲評論。 我沒有web.xml,我使用https://spring.io/guides/gs/rest-service/的例子,並沒有任何。
這裏是我的Application.class
package rest;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
你有'web.xml'嗎?也沒有提供用於組件掃描的軟件包。應該像'@ComponentScan(「rest」)',如果rest是你的基礎包。 – nebula
不,我沒有web.xml。更改爲@ComponentScan(「休息」)後,它仍然給出相同的錯誤...( – Bartek
)如果你沒有web.xml,那麼應該有一個實現了'WebApplicationInitializer'的類。你有這樣的類嗎? – nebula